r - 制作数据框每一列的向量并返回列表中的向量

我有一个像这样的数据框:

df<-data.frame(x=1:5,y=6:10,z=11:15)



 x  y  z
1 1  6 11
2 2  7 12
3 3  8 13
4 4  9 14
5 5 10 15

现在我想创建一个列表 new_list,其中每个元素都是作为向量的单个列。

这看起来像:

str(new_list)
list of 3
$ : num[1:5] 1 2 3 4 5
$ : num[1:5] 6 7 8 9 10
$ : num[1:5] 11 12 13 14 15

我试过:

new_list <-lapply(df,c)

但这只会返回

list of 3
$ : num[1:5] 1
$ : num[1:5] 6
$ : num[1:5] 11

注意:我希望我的目标很明确。如果没有,请告诉我。

最佳答案

使用 c

l <- lapply(df, c)
str(l)
# List of 3
#  $ x: int [1:5] 1 2 3 4 5
#  $ y: int [1:5] 6 7 8 9 10
#  $ z: int [1:5] 11 12 13 14 15

as.list

l <- as.list(df)
str(l)
# List of 3
#  $ x: int [1:5] 1 2 3 4 5
#  $ y: int [1:5] 6 7 8 9 10
#  $ z: int [1:5] 11 12 13 14 15

https://stackoverflow.com/questions/64210457/

相关文章:

reactjs - 如何触发事件 React 测试库

postgresql - Ecto:如何用不同的随机数更新所有记录

github - 使用 SonarQube(社区版)装饰 GitHub 中的拉取请求

terraform - 如何定义 "azurerm_resource_group_template_

swiftui - 编辑 : How we can alignment Image with Ima

ruby-on-rails - 使用 Cloudinary 和 Active Storage 时如何

unity3d - 如何将 Input.GetAxis ("Mouse X") 或 Input.Ge

python - 无法用pip安装pycuda

kubernetes - 使用 Persistent Volume Claim 时是否必须显式创建

r - 如何在保留 R 中的某些变量的同时将长整形为宽