将 data.frame 列转换为向量?

发布于 2024-11-29 23:56:04 字数 590 浏览 1 评论 0原文

我有一个数据框,例如:

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 的世界。此外,任何对这里发生的事情的解释都是值得赞赏的(即与 Python 或其他一些语言相关的解释会有所帮助!)

I have a dataframe such as:

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. Additionally, any explanation of what's going on here is appreciated (i.e. relating to Python or some other language would help!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

空宴 2024-12-06 23:56:04

我将尝试在不犯任何错误的情况下解释这一点,但我打赌这会在评论中引起一两个澄清。

数据框是一个列表。当您使用列名称和[对数据框进行子集化时,您得到的是一个子列表(或子数据框)。如果你想要实际的原子列,你可以使用 [[,或者有点令人困惑(对我来说))你可以做 aframe[,2] 它返回一个向量,而不是一个子列表。

所以尝试运行这个序列,也许事情会更清楚:

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

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

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

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.

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)
娇柔作态 2024-12-06 23:56:04

现在有一种简单的方法可以使用 dplyr 来完成此操作。

dplyr::pull(aframe, a2)

There's now an easy way to do this using dplyr.

dplyr::pull(aframe, a2)
成熟的代价 2024-12-06 23:56:04

您可以使用 $ 提取:

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

或双方括号:

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

You could use $ extraction:

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

or the double square bracket:

class(aframe[["a1"]])
[1] "numeric"
π浅易 2024-12-06 23:56:04

您不需要 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> 

You do not need as.vector(), but you do need correct indexing: avector <- aframe[ , "a2"]

The one other thing to be aware of is the drop=FALSE option to [:

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> 
临走之时 2024-12-06 23:56:04

你可以尝试这样的事情 -

as.vector(unlist(aframe$a2))

You can try something like this-

as.vector(unlist(aframe$a2))
糖粟与秋泊 2024-12-06 23:56:04

使用“[[”运算符的另一个优点是它可以与 data.frame 和 data.table 一起使用。因此,如果必须使该函数同时针对 data.frame 和 data.table 运行,并且您想从中提取一列作为向量,那么

data[["column_name"]] 

最好。

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.

坏尐絯 2024-12-06 23:56:04
as.vector(unlist(aframe['a2']))
as.vector(unlist(aframe['a2']))
唐婉 2024-12-06 23:56:04
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"
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"
迷路的信 2024-12-06 23:56:04

如果您只使用提取运算符,它就会起作用。默认情况下,[] 设置选项 drop=TRUE,这正是您想要的。有关更多详细信息,请参阅?'['

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

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"
℉絮湮 2024-12-06 23:56:04

我使用列表来过滤数据帧,根据它们是否具有列表中的值。

我一直通过将 1 列数据框导出到 Excel 来手动创建列表,在粘贴到 R 之前,我会在每个元素周围添加“”: list <- c("el1", "el2", ...) 这是通常后跟 FilteredData <- subset(Data, Column %in% list)。

在搜索 stackoverflow 并没有找到将 1 列数据帧转换为列表的直观方法之后,我现在发布我的第一个 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")

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")
我最亲爱的 2024-12-06 23:56:04

我们还可以将 data.frame 列一般转换为简单的向量。 as.vector 还不够,因为它保留了 data.frame 类和结构,因此我们还必须取出第一个(也是唯一的)元素:

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

到目前为止建议的所有解决方案都需要硬编码列标题。这使得它们成为非通用的(想象一下将其应用于函数参数)。

或者,您当然可以先从列中读取列名称,然后将它们插入其他解决方案的代码中。

We can also convert data.frame columns generically to a simple vector. 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:

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).

Alternatively, you could, of course read the column names from the column first and then insert them in the code in the other solutions.

贵在坚持 2024-12-06 23:56:04

另一种选择是将 as.matrixas.vector 结合使用。这可以针对一列完成,但如果您想将所有列转换为一个向量,也可以这样做。这是一个可重现的示例,首先将一列转换为向量,然后将完整的数据帧转换为一个向量:

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

创建于 2022 年 8 月 27 日,使用 reprex v2.0.2

Another option is using as.matrix with as.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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文