我有一个像这样的数据框:
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/
相关文章:
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