如何在满足行条件时扩展R数据框中的值?

huangapple go评论58阅读模式
英文:

How to extend values down an r dataframe when row conditions are met?

问题

请注意,以下是您要翻译的部分:

"Note that there are solutions to other questions that may resolve this specific question, such as https://stackoverflow.com/questions/40040834/replace-na-with-previous-or-next-value-by-group-using dplyr. However, this question isn't about replacing NA's, NA's are OK in this question in certain circumstances. This question addresses replacing all cells in a group in a dataframe that fall after the first non-NA in that group, with that first non-NA value. When I researched this issue I didn't find solutions that fit because I only want to replace NA's in certain circumstances (NA's in a group that occur prior to the first non-NA in that group remain; and a group with all NA's and no non-NA in that group retain all their NA's).

Is there a method, with a preference for dplyr or data.table, for extending target values down an R dataframe range when specified row conditions are met in a row within a group? I vaguely remember an rleid function in data.table that may do the trick but I'm having trouble implementing. Either as a new column or by over-writing existing column "State" in my below example.

For example, if we start with the below example dataframe, I'd like to send the target value of 1 in each row to the end of each ID grouping, after the first occurrence of that target value of 1 in a group, and as better explained in the illustration underneath:

myDF <- data.frame(
ID = c(1,1,1,1,2,2,2,3,3,3),
State = c(NA,NA,1,NA,1,NA,NA,NA,NA,NA))"

英文:

Note that there are solutions to other questions that may resolve this specific question, such as https://stackoverflow.com/questions/40040834/replace-na-with-previous-or-next-value-by-group-using-dplyr. However, this question isn't about replacing NA's, NA's are OK in this question in certain circumstances. This question addresses replacing all cells in a group in a dataframe that fall after the the first non-NA in that group, with that first non-NA value. When I researched this issue I didn't find solutions that fit because I only want to replace NA's in certain circumstances (NA's in a group that occur prior to the first non-NA in that group remain; and a group with all NA's and no non-NA in that group retain all their NA's).

Is there a method, with a preference for dplyr or data.table, for extending target values down an R dataframe range when specified row conditions are met in a row within a group? I vaguely remember an rleid function in data.table that may do the trick but I'm having trouble implementing. Either as a new column or by over-writing existing column "State" in my below example.

For example, if we start with the below example dataframe, I'd like to send the target value of 1 in each row to the end of each ID grouping, after the first occurrence of that target value of 1 in a group, and as better explained in the illustration underneath:

myDF &lt;- data.frame(
  ID = c(1,1,1,1,2,2,2,3,3,3),
  State = c(NA,NA,1,NA,1,NA,NA,NA,NA,NA))

如何在满足行条件时扩展R数据框中的值?

答案1

得分: 1

你可以使用 fill 函数:

library(tidyr)
myDF %>%
  group_by(ID) %>%
  fill(State, .direction = "down")
# A tibble: 10 × 2
# Groups:   ID [3]
      ID State
   <dbl> <dbl>
 1     1    NA
 2     1    NA
 3     1     1
 4     1     1
 5     2     1
 6     2     1
 7     2     1
 8     3    NA
 9     3    NA
10     3    NA
英文:

You can use fill:

library(tidyr)
myDF %&gt;%
  group_by(ID) %&gt;%
  fill(State, .direction = &quot;down&quot;)
# A tibble: 10 &#215; 2
# Groups:   ID [3]
      ID State
   &lt;dbl&gt; &lt;dbl&gt;
 1     1    NA
 2     1    NA
 3     1     1
 4     1     1
 5     2     1
 6     2     1
 7     2     1
 8     3    NA
 9     3    NA
10     3    NA

huangapple
  • 本文由 发表于 2023年2月19日 20:30:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定