ddply具有固定的行数 [英] ddply with fixed number of rows
本文介绍了ddply具有固定的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按行数"分解数据.也就是说,我想向函数发送固定数量的行,并且当我到达数据帧的末尾(最后一个块)时,无论是否具有固定的行数或更少的行数,我都只需要发送该块.像这样:
I want to break up my data by 'number of rows'. That is to say I want to send a fixed number of rows to my function and when I get to the end of the data frame (last chunk) I need to just send the chunk whether it has the fixed number of rows or less. Something like this:
ddply(df, .(8 rows), .fun=somefunction)
推荐答案
如果要使用plyr
,则可以添加类别列:
If you want to use plyr
you can add a category column:
df <- data.frame(x=rnorm(100), y=rnorm(100))
somefunction <- function(df) {
data.frame(mean(df$x), mean(df$y))
}
df$category <- rep(letters[1:10], each=10)
ddply(df, .(category), somefunction)
但是,在这种情况下,申请家庭可能是一个更好的选择:
But, the apply family might be a better option in this case:
somefunction <- function(n, x, y) {
data.frame(mean(x[n:(n+9)]), mean(y[n:n+9]))
}
lapply(seq(1, nrow(df), by=10), somefunction, x=df$x, y=df$y)
这篇关于ddply具有固定的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文