将数据表拆分为小表 R

use*_*931 5 split r dataframe

我有一个下表(超过 1k 行):

  x1      x2  x3  x4 
7809  243638   1   1 
7809  243638   1   1
7809  243638   1   1 
... 
3453  222222   1   0
Run Code Online (Sandbox Code Playgroud)

我需要根据第二列将此表拆分为小表(将在我的环境中作为数据框)x2。我尝试这样做split(dat,dat$x2),R 做得对,但在列表中。

rmu*_*uc8 6

如果你这样做

split_list <- split(dat,dat$x2)
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令访问分割结果

split_list[[1]]
split_list[[2]]
....
Run Code Online (Sandbox Code Playgroud)

转换结果

# to a data.frame
df1 <- as.data.frame(split_list[[1]])

# to a table
t1 <- as.table(split_list[[1]])
Run Code Online (Sandbox Code Playgroud)

存储在多个数据集中(尽管我没有看到它的好处)

names1 <- names(split_list)

for(i in seq_along(names1)){
  assign(names1[i], split_list[[i]])
}
Run Code Online (Sandbox Code Playgroud)