R:将数据帧按组和行迭代拆分为列表 [英] R: Split dataframe into list by group and row iteration
问题描述
我有一个像这样的数据框:
I have a dataframe like so:
set.seed(34)
startingframe <- data.frame(
group1=factor(rep(c("a","b"),each=3,times=1)),
time=rep(1:3,each=1,times=2),
othercolumn=rnorm(1:6)
)
...我正在尝试将其处理成一个列表,该列表按组和time
列的每个迭代"划分,结果如下:
...which I am trying to manipulate into a list that splits by group and each 'iteration' of the time
column, results looking like so:
$a1
group1 time othercolumn
1 a 1 -0.13889
$a2
group1 time othercolumn
1 a 1 -0.138890
2 a 2 1.199813
$a3
group1 time othercolumn
1 a 1 -0.1388900
2 a 2 1.1998129
3 a 3 -0.7477224
$b1
group1 time othercolumn
4 b 1 -0.5752482
$b2
group1 time othercolumn
4 b 1 -0.5752482
5 b 2 -0.2635815
$b3
group1 time othercolumn
4 b 1 -0.5752482
5 b 2 -0.2635815
6 b 3 -0.4554921
我想我只需要将迭代位合并到此代码中即可:
I think I just need the iteration bit incorporated into this code:
split_list <- split(startingframe,list(startingframe$group1,startingframe$time))
我可能已经简化了原始问题,但是实际数据集还有其他一些列需要在列表中传递.
I may have oversimplified the original problem, but the real dataset has additional columns that would need to be brought through in the list.
推荐答案
您可以将head
用于序列1:nrow(x)
中的每个数字,数据的每个子组x
You could use head
for every number in the sequence 1:nrow(x)
, for each sub-group x
of your data
out <- lapply(split(df, df$group1), function(x) lapply(1:nrow(x), head, x = x))
out <- unlist(out, recursive = F)
out
# $`a1`
# group1 time
# 1: a 1
#
# $a2
# group1 time
# 1: a 1
# 2: a 2
#
# $a3
# group1 time
# 1: a 1
# 2: a 2
# 3: a 3
#
# $b1
# group1 time
# 1: b 1
#
# $b2
# group1 time
# 1: b 1
# 2: b 2
#
# $b3
# group1 time
# 1: b 1
# 2: b 2
# 3: b 3
尽管您可能不需要创建所有这些数据框的列表,而只需创建索引列表并根据需要使用它们即可.
You might not need to create a list of all these dataframes though, you could just create a list of indices and use them as needed.
inds <-
lapply(split(seq(nrow(df)), df$group1), function(x)
lapply(1:length(x), function(y) x[seq(y)]))
inds
# $`a`
# $`a`[[1]]
# [1] 1
#
# $`a`[[2]]
# [1] 1 2
#
# $`a`[[3]]
# [1] 1 2 3
#
#
# $b
# $b[[1]]
# [1] 4
#
# $b[[2]]
# [1] 4 5
#
# $b[[3]]
# [1] 4 5 6
df[inds$b[[2]]]
# group1 time
# 1: b 1
# 2: b 2
这篇关于R:将数据帧按组和行迭代拆分为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!