[英]How to replace NAs with previous column values plus one by group based on other columns in R?
I am currently trying to replace NA values in my dataframe with the previous value plus one.我目前正在尝试用以前的值加一替换我的数据框中的 NA 值。 However, there is a condition in that the values must never exceed 52 due to that being the number of weeks within a calendar year.
但是,存在一个条件,因为这是一个日历年中的周数,因此这些值不得超过 52。 Here's an example of the the dataframe below:
以下是以下数据框的示例:
Animal Age Week
Dog 13 5
Dog 14 6
Dog 15 7
Dog 16 NA
Dog 17 NA
Cat 12 46
Cat 13 47
Cat 14 48
Cat 15 49
Cat 16 50
Cat 17 NA
Rat 10 49
Rat 11 50
Rat 12 51
Rat 13 NA
Rat 14 NA
Rat 15 NA
Rat 16 NA
Rat 17 NA
What I would like the code to output is the following below:我希望输出的代码如下:
Animal Age Week
Dog 13 5
Dog 14 6
Dog 15 7
Dog 16 8
Dog 17 9
Cat 12 46
Cat 13 47
Cat 14 48
Cat 15 49
Cat 16 50
Cat 17 51
Rat 10 49
Rat 11 50
Rat 12 51
Rat 13 52
Rat 14 1
Rat 15 2
Rat 16 3
Rat 17 4
The caveat is that the end age of each animal will always be 17. I tried using R's function "Complete" and "Fill", but I could not find a way to add plus one with the condition that it resets after week 52. Any help would be appreciated.需要注意的是,每只动物的最终年龄总是 17 岁。我尝试使用 R 的函数“完成”和“填充”,但我找不到在第 52 周后重置的情况下添加加一的方法。任何帮助将不胜感激。
For each group ( Animal
), we add the first Week
number to row number and get the remainder value.对于每个组 (
Animal
),我们将第一个Week
数添加到行号并获得余数。 We finally replace
the 0 value with 52.我们最终
replace
0 值replace
为 52。
library(dplyr)
df %>%
group_by(Animal) %>%
mutate(Week = (first(Week) + row_number() - 1) %% 52,
Week = replace(Week, Week == 0, 52))
# Animal Age Week
# <fct> <int> <dbl>
# 1 Dog 13 5
# 2 Dog 14 6
# 3 Dog 15 7
# 4 Dog 16 8
# 5 Dog 17 9
# 6 Cat 12 46
# 7 Cat 13 47
# 8 Cat 14 48
# 9 Cat 15 49
#10 Cat 16 50
#11 Cat 17 51
#12 Rat 10 49
#13 Rat 11 50
#14 Rat 12 51
#15 Rat 13 52
#16 Rat 14 1
#17 Rat 15 2
#18 Rat 16 3
#19 Rat 17 4
Similarly, in base R :同样,在基数 R 中:
df <- transform(df, Week = ave(Week, Animal, FUN = function(x)
seq_along(x) + x[1] - 1 %% 52))
transform(df, Week = replace(Week, Week == 0, 52))
We can use data.table
我们可以使用
data.table
library(data.table)
setDT(df)[, Week := (first(Week) + .N - 1) %% 52, Animal][Week == 0, Week := 52][]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.