计算R中行中的连续值 [英] Counting consecutive values in rows in R
问题描述
我在第一列中有一个时间序列和面板数据数据帧,其中带有特定的ID,并且具有每周工作状态:失业(1),受雇(0).
I have a time series and panel data data frame with a specific ID in the first column, and a weekly status for employment: Unemployed (1), employed (0).
我有261个变量(每年的星期)和1.000.000个观测值.
I have 261 variables (the weeks every year) and 1.000.000 observations.
我想计算R中每一行连续出现"1"的最大次数.
I would like to count the maximum number of times '1' occurs consecutively for every row in R.
我稍微看过rowSums和rle(),但是我对行的总和不感兴趣,因为值的连续性非常重要.
I have looked a bit at rowSums and rle(), but I am not as far as I know interested in the sum of the row, as it is very important the values are consecutive.
推荐答案
我们可以编写一个辅助函数,以返回向量中某个值连续重复的最大次数,默认值为1
(针对此用例)
We can write a little helper function to return the maximum number of times a certain value is consecutively repeated in a vector, with a nice default value of 1
for this use case
most_consecutive_val = function(x, val = 1) {
with(rle(x), max(lengths[values == val]))
}
然后我们可以将该函数应用
到数据框的行,删除第一列(以及不应包含的任何其他列):
Then we can apply
this function to the rows of your data frame, dropping the first column (and any other columns that shouldn't be included):
apply(your_data_frame[-1], MARGIN = 1, most_consecutive_val)
如果您共享一些易于导入的示例数据,则在出现问题的情况下,我们很乐意帮助您进行调试. dput
是共享数据可复制/可粘贴子集的简便方法,例如 dput(your_data [1:5,1:10])
共享数据的前5行和10列.
If you share some easily imported sample data, I'll be happy to help debug in case there are issues. dput
is an easy way to share a copy/pasteable subset of data, for example dput(your_data[1:5, 1:10])
would be a great way to share the first 5 rows and 10 columns of your data.
如果要避免出现警告,并且在没有1的情况下出现 -Inf
结果,请使用注释中Ryan的建议:
If you want to avoid warnings and -Inf
results in the case where there are no 1s, use Ryan's suggestion from the comments:
most_consecutive_val = function(x, val = 1) {
with(rle(x), if(all(values != val)) 0 else max(lengths[values == val]))
}
这篇关于计算R中行中的连续值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!