简体   繁体   English

将 data.frame 列转换为向量?

[英]Convert data.frame column to a vector?

I have a dataframe such as:我有一个 dataframe 例如:

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)

I tried the following to convert one of the columns to a vector, but it doesn't work:我尝试了以下方法将其中一列转换为向量,但它不起作用:

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

This is the only solution I could come up with, but I'm assuming there has to be a better way to do this:这是我能想到的唯一解决方案,但我假设必须有更好的方法来做到这一点:

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

Note: My vocabulary above may be off, so please correct me if so.注意:我上面的词汇可能有问题,如果有,请纠正我。 I'm still learning the world of R.我还在学习 R 的世界。 Additionally, any explanation of what's going on here is appreciated (ie relating to Python or some other language would help!)此外,对这里发生的事情的任何解释表示赞赏(即与 Python 或其他一些语言有关!)

I'm going to attempt to explain this without making any mistakes, but I'm betting this will attract a clarification or two in the comments.我将尝试在不犯任何错误的情况下解释这一点,但我打赌这将在评论中引起一两个澄清。

A data frame is a list.数据框是一个列表。 When you subset a data frame using the name of a column and [ , what you're getting is a sublist (or a sub data frame).当您使用列名和[对数据框进行子集化时,您将得到一个子列表(或子数据框)。 If you want the actual atomic column, you could use [[ , or somewhat confusingly (to me) you could do aframe[,2] which returns a vector, not a sublist.如果你想要实际的原子列,你可以使用[[ ,或者有点令人困惑(对我来说)你可以做aframe[,2]它返回一个向量,而不是一个子列表。

So try running this sequence and maybe things will be clearer:所以尝试运行这个序列,也许事情会更清楚:

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

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

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

There's now an easy way to do this using dplyr .现在有一种简单的方法可以使用dplyr来做到这一点。

dplyr::pull(aframe, a2)

You could use $ extraction:您可以使用$提取:

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

or the double square bracket:或双方括号:

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

You do not need as.vector() , but you do need correct indexing: avector <- aframe[, "a2"]您不需要as.vector() ,但确实需要正确的索引: avector <- aframe[, "a2"]

The one other thing to be aware of is the drop=FALSE option to [ :要注意的另一件事是[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> 

You can try something like this-你可以试试这样的 -

as.vector(unlist(aframe$a2))

Another advantage of using the '[[' operator is that it works both with data.frame and data.table.使用 '[[' 运算符的另一个优点是它同时适用于 data.frame 和 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因此,如果 function 必须为 data.frame 和 data.table 运行,并且您想从中提取一列作为向量然后

data[["column_name"]] 

is best.是最好的。

as.vector(unlist(aframe['a2']))
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"

If you just use the extract operator it will work.如果您只使用提取运算符,它将起作用。 By default, [] sets option drop=TRUE , which is what you want here.默认情况下, [] 设置选项drop=TRUE ,这就是您想要的。 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"

I use lists to filter dataframes by whether or not they have a value %in% a list.我使用列表来过滤数据框,它们是否具有 %in% 列表的值。

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).我一直通过将 1 列 dataframe 导出到 Excel 来手动创建列表,在其中我将在每个元素周围添加“”,然后粘贴到 Rel 中,然后再粘贴到 Rel:其次是 FilteredData <- 子集(数据,列 %in% 列表)。

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:在搜索了 stackoverflow 并没有找到将 1 列 dataframe 转换为列表的直观方法后,我现在发布我的第一个 stackoverflow 贡献:

# 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")

We can also convert data.frame columns generically to a simple vector.我们还可以将 data.frame 列一般地转换为简单的向量。 as.vector is not enough as it retains the data.frame class and structure, so we also have to pull out the first (and only) element: as.vector是不够的,因为它保留了 data.frame class 和结构,所以我们还必须拉出第一个(也是唯一一个)元素:

df_column_object <- aframe[,2]
simple_column <- df_column_object[[1]]

All the solutions suggested so far require hardcoding column titles.到目前为止建议的所有解决方案都需要对列标题进行硬编码。 This makes them non-generic (imagine applying this to function arguments).这使得它们非泛型(想象将其应用于 function 参数)。

Alternatively, you could, of course read the column names from the column first and then insert them in the code in the other solutions.或者,您当然可以先从列中读取列名,然后将它们插入到其他解决方案的代码中。

Another option is using as.matrix with as.vector .另一种选择是将as.matrixas.vector一起使用。 This can be done for one column but is also possible if you want to convert all columns to one vector.这可以针对一列完成,但如果要将所有列转换为一个向量,也可以这样做。 Here is a reproducible example with first converting one column to a vector and second convert complete dataframe to one vector:这是一个可重现的示例,首先将一列转换为向量,然后将完整的 dataframe 转换为一个向量:

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)

# Convert one column to vector
avector <- as.vector(as.matrix(aframe[,"a2"]))
class(avector)
#> [1] "numeric"
avector
#> [1]  6  7  8  9 10

# Convert all columns to one vector
avector <- as.vector(as.matrix(aframe))
class(avector)
#> [1] "numeric"
avector
#>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

Created on 2022-08-27 with reprex v2.0.2使用reprex v2.0.2创建于 2022-08-27

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM