Programing

data.frame 열을 벡터로 변환 하시겠습니까?

lottogame 2020. 6. 25. 08:04
반응형

data.frame 열을 벡터로 변환 하시겠습니까?


다음과 같은 데이터 프레임이 있습니다.

a1 = c(1, 2, 3, 4, 5)
a2 = c(6, 7, 8, 9, 10)
a3 = c(11, 12, 13, 14, 15)
aframe = data.frame(a1, a2, a3)

열 중 하나를 벡터로 변환하기 위해 다음을 시도했지만 작동하지 않습니다.

avector <- as.vector(aframe['a2'])
class(avector) 
[1] "data.frame"

이것이 내가 취할 수있는 유일한 해결책이지만, 이것을 수행하는 더 좋은 방법이 있어야한다고 가정합니다.

class(aframe['a2']) 
[1] "data.frame"
avector = c()
for(atmp in aframe['a2']) { avector <- atmp }
class(avector)
[1] "numeric"

참고 : 위의 어휘가 꺼져있을 수 있으므로 수정하십시오. 나는 여전히 R의 세계를 배우고 있습니다. 또한, 여기서 무슨 일이 일어나고 있는지에 대한 설명은 높이 평가됩니다 (즉, 파이썬이나 다른 언어와 관련이 있으면 도움이 될 것입니다!)


나는 실수를하지 않고 이것을 설명하려고 노력할 것이지만, 나는 이것이 의견에서 명확하게 하나 또는 두 개를 끌 것이라고 내기 할 것입니다.

데이터 프레임은 목록입니다. 열의 이름을 사용하여 데이터 프레임을 부분 집합 화 [하고받는 것은 하위 목록 (또는 하위 데이터 프레임)입니다. 실제 원자 열을 원한다면 하위 목록이 아닌 벡터를 반환하는 [[을 사용할 수 aframe[,2]있습니다.

따라서이 시퀀스를 실행하면 상황이 더 명확해질 수 있습니다.

avector <- as.vector(aframe['a2'])
class(avector) 

avector <- aframe[['a2']]
class(avector)

avector <- aframe[,2]
class(avector)

$추출을 사용할 수 있습니다 .

class(aframe$a1)
[1] "numeric"

또는 이중 대괄호 :

class(aframe[["a1"]])
[1] "numeric"

를 사용하여이 작업을 수행하는 쉬운 방법이 dplyr있습니다.

dplyr::pull(aframe, a2)

필요 as.vector()는 없지만 올바른 색인 생성이 필요합니다.avector <- aframe[ , "a2"]

알아야 할 또 다른 사항은 다음과 같은 drop=FALSE옵션입니다 [.

R> aframe <- data.frame(a1=c1:5, a2=6:10, a3=11:15)
R> aframe
  a1 a2 a3
1  1  6 11
2  2  7 12
3  3  8 13
4  4  9 14
5  5 10 15
R> avector <- aframe[, "a2"]
R> avector
[1]  6  7  8  9 10
R> avector <- aframe[, "a2", drop=FALSE]
R> avector
  a2
1  6
2  7
3  8
4  9
5 10
R> 

Another advantage of using the '[[' operator is that it works both with data.frame and data.table. So if the function has to be made running for both data.frame and data.table, and you want to extract a column from it as a vector then

data[["column_name"]] 

is best.


You can try something like this-

as.vector(unlist(aframe$a2))

If you just use the extract operator it will work. By default, [] sets option drop=TRUE, which is what you want here. See ?'[' for more details.

>  a1 = c(1, 2, 3, 4, 5)
>  a2 = c(6, 7, 8, 9, 10)
>  a3 = c(11, 12, 13, 14, 15)
>  aframe = data.frame(a1, a2, a3)
> aframe[,'a2']
[1]  6  7  8  9 10
> class(aframe[,'a2'])
[1] "numeric"

a1 = c(1, 2, 3, 4, 5)
a2 = c(6, 7, 8, 9, 10)
a3 = c(11, 12, 13, 14, 15)
aframe = data.frame(a1, a2, a3)
avector <- as.vector(aframe['a2'])

avector<-unlist(avector)
#this will return a vector of type "integer"

I use lists to filter dataframes by whether or not they have a value %in% a list.

I had been manually creating lists by exporting a 1 column dataframe to Excel where I would add " ", around each element, before pasting into R: list <- c("el1", "el2", ...) which was usually followed by FilteredData <- subset(Data, Column %in% list).

After searching stackoverflow and not finding an intuitive way to convert a 1 column dataframe into a list, I am now posting my first ever stackoverflow contribution:

# assuming you have a 1 column dataframe called "df"
list <- c()
for(i in 1:nrow(df)){
  list <- append(list, df[i,1])
}
View(list)
# This list is not a dataframe, it is a list of values
# You can filter a dataframe using "subset([Data], [Column] %in% list")

참고URL : https://stackoverflow.com/questions/7070173/convert-data-frame-column-to-a-vector

반응형