R의 벡터 및 목록 데이터 유형의 차이점은 무엇입니까?
R에서 벡터 및 목록 데이터 유형의 주요 차이점은 무엇입니까? 이 두 데이터 유형을 사용하는 경우의 장단점은 무엇입니까?
데이터 유형의 사용 사례를 보여주는 예를 보는 것이 좋습니다.
기술적으로 목록 은 벡터이지만 그 용어를 사용하는 사람은 거의 없습니다. "목록"은 여러 가지 모드 중 하나이며 다른 모드는 "논리", "문자", "숫자", "정수"입니다. 당신이 부르는 것은 엄격한 R 용어에서 "원자"입니다 :
aaa <- vector("list", 3)
is.list(aaa) #TRUE
is.vector(aaa) #TRUE
리스트는 "재귀"유형이지만 원자 벡터는 그렇지 않습니다.
is.recursive(aaa) # TRUE
is.atomic(aaa) # FALSE
재귀 적이든 원자 적이든 차원 속성 (매트릭스 및 배열)이 있는지에 따라 다른 기능을 가진 데이터 오브젝트를 처리합니다. 그러나 다른 데이터 구조의 "장점과 단점"에 대한 논의가 SO에 대해 충분히 집중된 질문인지 확실하지 않습니다. Tommy가 말한 것에 덧붙여, 임의의 수의 다른 벡터를 보유 할 수있는 목록 외에도 구조를 정의하는 차원 속성을 가진 특정 유형의 목록 인 데이터 프레임을 사용할 수 있습니다. 실제로 접힌 원자 객체 인 행렬 및 배열과 달리 데이터 프레임에는 요인 유형을 포함하여 다양한 유형을 보유 할 수 있습니다.
목록은 "재귀 적"입니다. 이는 다른 유형의 값, 다른 목록도 포함 할 수 있음을 의미합니다.
x <- list(values=sin(1:3), ids=letters[1:3], sub=list(foo=42,bar=13))
x # print the list
x$values # Get one element
x[["ids"]] # Another way to get an element
x$sub$foo # Get sub elements
x[[c(3,2)]] # Another way (gets 13)
str(x) # A "summary" of the list's content
목록은 R에서 데이터 세트를 나타내는 데 사용됩니다. data.frame
클래스는 기본적으로 각 요소가 특정 유형의 열인 목록입니다.
또 다른 용도는 모델을 나타낼 때입니다. 결과 lm
는 유용한 객체가 많이 포함 된 목록을 반환합니다.
d <- data.frame(a=11:13, b=21:23)
is.list(d) # TRUE
str(d)
m <- lm(a ~ b, data=d)
is.list(m) # TRUE
str(m)
원자 벡터 (목록과 같지 않지만 숫자, 논리 및 문자)는 모든 요소가 동일한 유형을 갖는 것으로 알려져 있기 때문에 유용합니다. 이것은 그것들을 매우 빠르게 조작하게합니다.
R에 들어 갔지만 C / Java / Ruby / PHP / Python 배경에서 온 사람으로서 여기에 내가 어떻게 생각하는지가 있습니다.
A list
는 실제로 배열 + 해시 맵입니다. PHP 연관 배열입니다.
> foo = list(bar='baz')
> foo[1]
'baz'
> foo$bar
'baz'
> foo[['bar']]
'baz'
A vector
는 고정형 배열 / 목록입니다. 다른리스트를 링크리스트에 넣는 것은 어쨌든 안티 패턴이기 때문에 그것을 링크리스트처럼 생각하십시오. SIMD / MMX / 벡터 단위가 단어를 사용하는 것과 같은 의미의 벡터입니다.
이 질문과 비슷한 입문 질문은 http://www.burns-stat.com/pages/Tutor/hints_R_begin.html 에서 답변됩니다 .
R을 가능한 빨리 시작하고 실행하는 부드러운 소개입니다. 어느 정도는 성공합니다.
--- 편집하다: --
더 설명하려는 시도; 위의 참조에서 인용.
원자 벡터
발생할 수있는 원자 벡터에는 세 가지 종류가 있습니다.
- "숫자"
- “논리적”
- "캐릭터"
원자 벡터에 대해 기억해야 할 것은 원자 벡터의 모든 요소가 하나의 유형에 불과하다는 것입니다.
명부
Lists can have different types of items in different components. A component of a list is allowed to be another list , an atomic vector (and other things).
Please also refer to this link.
Vectors (one-dimensional array): can hold numeric, character or logical values. The elements in a vector all have the same data type.
A list in R is similar to your to-do list at work or school: the different items on that list most likely differ in length, characteristic, type of activity that has to do be done, ...
A list in R allows you to gather a variety of objects under one name (that is, the name of the list) in an ordered way. These objects can be matrices, vectors, data frames, even other lists, etc. It is not even required that these objects are related to each other in any way.
You could say that a list is some kind super data type: you can store practically any piece of information in it!
To construct a list you use the function list(): my_list <- list(comp1, comp2 ...)
The arguments to the list function are the list components. Remember, these components can be matrices, vectors, other lists, ...
To summarize, the main difference between list and vector is that list in R allows you to gather a variety of objects under one name (that is, the name of the list) in an ordered way. These objects can be matrices, vectors, data frames, even other lists, etc. It is not even required that these objects are related to each other in any way... while the elements in a vector all have the same data type.
list include multiple data types like character, numeric, logical et. but vector only contains similar type of data. for ex:
scores <- c(20,30,40,50)
student <- c("A","B","C","D")
sc_log <- c(TRUE,FALSE,FALSE,TRUE)
for list:
mylist <- list(scores,student,sc_log)
# search for class of mylist vector
#check structure of mylist using str() function.
str(mylist)
[1] list of 3
[1] $:num [1:4] 20 30 40 50
[2] $:chr [1:4] "A""B""C""D"
[3] $:log [1:4] TRUE FALSE FALSE TRUE
which means list containing multiple data types like numeric, character and logical in mylist.But in vector there will be single data type of all elements in that vector
for ex:
for vector:
vector1 <- c(1,2,3,4)
Class(vector1)
[1] "Numeric"
#which means all elements of vector containing single data type that is numeric only.
'development' 카테고리의 다른 글
스레드에 고유 힙이 있습니까? (0) | 2020.07.28 |
---|---|
Java 삼항 연산자 대 if / else in (0) | 2020.07.28 |
NaN (결측) 값이있는 그룹 별 열 (0) | 2020.07.28 |
스위프트는 반사를 지원합니까? (0) | 2020.07.28 |
C ++ 열거 형을 문자열에 쉽게 매핑하는 방법 (0) | 2020.07.28 |