![](/img/trans.png)
[英]How to create a list of data.frame with matched rows and columns in R
[英]R data.frame columns into single list
盡管我確定它已經被問過了,但我似乎找不到答案......但我想提取 data.frame 列的子集並將其組合到一個列表中,而不使用任何包。 例如:
DF <- data.frame(a = c(1,2,3), b = c(4,5,6), c = c(7,8,9))
test <- c(DF[,1], DF[,2], DF[,3]) # Gives what I want
test <- c(DF[,1:3]) # Doesn't give what I want
所以想要的 output 將是一個值 1:9 的列表。 我希望動態分配子集,因此即使它提供了我想要的 output,我也不能按照我輸入的方式進行操作。 我能想到的唯一另一種方法是有一個循環並一次遞增地添加一列,例如 test <- c(test, DF[,i])
也許您可以在do.call
中嘗試c
test <- do.call(c,DF)
這樣
> test
a1 a2 a3 b1 b2 b3 c1 c2 c3
1 2 3 4 5 6 7 8 9
您可以unlist
:
unlist(DF)
# a1 a2 a3 b1 b2 b3 c1 c2 c3
# 1 2 3 4 5 6 7 8 9
並在需要時擺脫名稱:
unlist(DF, use.names = FALSE) # or also unname(unlist(DF))
# [1] 1 2 3 4 5 6 7 8 9
purrr
中的flatten
選項
library(dplyr)
library(purrr)
DF %>%
flatten_dbl
#[1] 1 2 3 4 5 6 7 8 9
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.