简体   繁体   English

如何根据R中的其他列将NA替换为先前的列值加上一个分组?

[英]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.

相关问题 R:如何使用来自利用其他多列的条件的值替换 dataframe 列中的 NA? - R: How do I replace NAs in a dataframe column with values from conditions leveraging other multiple columns? 如何根据R中另一列中的值替换数据框的列中的值? - How to replace values in the columns of a dataframe based on the values in the other column in R? 如何根据其他列R中的值对一列中的值求和? - How to sum values in one column based on values in other columns R? 在 R 中:将 NA 替换为其他行的值,但其他列中的值相同 - In R: Replace NAs with values of other row but same value in other column 如何根据 R 中多个其他列的 NA 和零创建新列? - How to create a new column based on the NAs and ZEROs of multiple other columns in R? 如何基于其他列按列获取新列中的值 - how to get values in new column based on other column group by columns 根据另一列的值替换列中的 NA - Replace NAs in a column based on values of another column R:如何根据多个其他列在一个列中查找不同的值 - R: How to find differing values in one column based on multiple other columns 如何根据R中的/另一列替换几列的值? - How to replace values of several columns based on/ another column in R? 如何根据 R 中其他列的值过滤一列中的值? - How to filter for value in one column based on values from other columns in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM