如何拆分值的数据帧并在块上使用 rle?
How to split a dataframe of values and use rle on the chunks?
我正在尝试划分(不一定要分成偶数块,因为实际数据可能会有所不同)单列整数称为分数(.csv 文件),然后计算连续值(x 所选值,例如1) 在每个分割部分或连续值的平均长度。 rle 一切皆有可能。
我可以使用 split 轻松拆分整数列,但这似乎与 rle 不兼容(大概 bc split 生成一个列表)。我寻找 and/or 替代 rle 的解决方案,但没有想出任何办法。
示例分数
scores <- c(1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1)
分开他们
g <- seq_along(scores)
scores.div <- split(scores, ceiling(g/7))
我尝试过但没有奏效的示例
Scores.rle <- sapply(scores.div, function(x) {
r <- rle(x)
sum(r$values == 1)
})
我希望得到这样的输出:
2 2 0 1 1
非常感谢任何帮助
我们也可以使用tapply
as.vector(tapply(scores, ceiling(g/7), FUN = function(x) sum(rle(x)$values == 1)))
#[1] 2 2 0 1 1
我运行你的代码和你的代码运行良好。
> scores <- c(1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1)
> g <- seq_along(scores)
> scores.div <- split(scores, ceiling(g/7))
> Scores.rle <- sapply(scores.div, function(x) {
+ r <- rle(x)
+ sum(r$values == 1)
+ })
> Scores.rle
1 2 3 4 5
2 2 0 1 1
我的会话是:
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936 LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1
如 docs 中所述,sapply
returns 一个命名向量,根据 lapply
:
创建的列表的名称
sapply
is a user-friendly version and wrapper of lapply
by default returning a vector, matrix or, if simplify = "array"
, an array if appropriate, by applying simplify2array()
. sapply(x, f, simplify = FALSE, USE.NAMES = FALSE)
is the same as lapply(x, f)
只需unname
它(查看它的docs)就大功告成了:
> scores <- c(1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1)
> g <- seq_along(scores)
> scores.div <- split(scores, ceiling(g/7))
> unname(sapply(scores.div, function(x) sum(rle(x)$values ==1)))
[1] 2 2 0 1 1
我正在尝试划分(不一定要分成偶数块,因为实际数据可能会有所不同)单列整数称为分数(.csv 文件),然后计算连续值(x 所选值,例如1) 在每个分割部分或连续值的平均长度。 rle 一切皆有可能。
我可以使用 split 轻松拆分整数列,但这似乎与 rle 不兼容(大概 bc split 生成一个列表)。我寻找 and/or 替代 rle 的解决方案,但没有想出任何办法。
示例分数
scores <- c(1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1)
分开他们
g <- seq_along(scores)
scores.div <- split(scores, ceiling(g/7))
我尝试过但没有奏效的示例
Scores.rle <- sapply(scores.div, function(x) {
r <- rle(x)
sum(r$values == 1)
})
我希望得到这样的输出:
2 2 0 1 1
非常感谢任何帮助
我们也可以使用tapply
as.vector(tapply(scores, ceiling(g/7), FUN = function(x) sum(rle(x)$values == 1)))
#[1] 2 2 0 1 1
我运行你的代码和你的代码运行良好。
> scores <- c(1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1)
> g <- seq_along(scores)
> scores.div <- split(scores, ceiling(g/7))
> Scores.rle <- sapply(scores.div, function(x) {
+ r <- rle(x)
+ sum(r$values == 1)
+ })
> Scores.rle
1 2 3 4 5
2 2 0 1 1
我的会话是:
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936 LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1
如 docs 中所述,sapply
returns 一个命名向量,根据 lapply
:
sapply
is a user-friendly version and wrapper oflapply
by default returning a vector, matrix or, ifsimplify = "array"
, an array if appropriate, by applyingsimplify2array()
.sapply(x, f, simplify = FALSE, USE.NAMES = FALSE)
is the same aslapply(x, f)
只需unname
它(查看它的docs)就大功告成了:
> scores <- c(1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1)
> g <- seq_along(scores)
> scores.div <- split(scores, ceiling(g/7))
> unname(sapply(scores.div, function(x) sum(rle(x)$values ==1)))
[1] 2 2 0 1 1