问题描述
有很多解决方案可以拆分数据框,例如
There are many answers for how to split a dataframe, for example How to split a data frame?
但是,我想拆分一个数据框,使较小的数据框包含上一个数据帧的最后一行,第一行以下数据框。
However, I'd like to split a dataframe so that the smaller dataframes contain the last row of the previous dataframe and the first row of the following dataframe.
这是一个例子
n <- 1:9
group <- rep(c("a","b","c"), each = 3)
data.frame(n = n, group)
n group
1 1 a
2 2 a
3 3 a
4 4 b
5 5 b
6 6 b
7 7 c
8 8 c
9 9 c
像输出一样:
d1 <- data.frame(n = 1:4, group = c(rep("a",3),"b"))
d2 <- data.frame(n = 3:7, group = c("a",rep("b",3),"c"))
d3 <- data.frame(n = 6:9, group = c("b",rep("c",3)))
d <- list(d1, d2, d3)
d
[[1]]
n group
1 1 a
2 2 a
3 3 a
4 4 b
[[2]]
n group
1 3 a
2 4 b
3 5 b
4 6 b
5 7 c
[[3]]
n group
1 6 b
2 7 c
3 8 c
4 9 c
什么是完成此任务的有效方法?
What is an efficient way to accomplish this task?
推荐答案
假设 DF
是原始的data.frame,列 n
和组
。让 n
成为 DF
中的行数。现在定义一个函数 extract
,它给出了索引序列 ix
放大它以包括在第一个之前和之后的索引最后一个,然后返回 DF
的那些行。现在我们已经定义了 extract
,按组分割向量1,...,n,并将 extract
应用于每个
Suppose DF
is the original data.frame, the one with columns n
and group
. Let n
be the number of rows in DF
. Now define a function extract
which given a sequence of indexes ix
enlarges it to include the one prior to the first and after the last and then returns those rows of DF
. Now that we have defined extract
, split the vector 1, ..., n by group and apply extract
to each component of the split.
n <- nrow(DF)
extract <- function(ix) DF[seq(max(1, min(ix) - 1), min(n, max(ix) + 1)), ]
lapply(split(seq_len(n), DF$group), extract)
$a
n group
1 1 a
2 2 a
3 3 a
4 4 b
$b
n group
3 3 a
4 4 b
5 5 b
6 6 b
7 7 c
$c
n group
6 6 b
7 7 c
8 8 c
9 9 c
这篇关于在R中,分割数据帧,因此子集数据帧包含先前数据帧的最后一行和后续数据帧的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!