我正在尝试通过按行对现有列应用一些条件来生成新列:
“生成的”列应显示该特定ID的到该月的最大值
Data:
ID Month Value
1 Apr 2
1 May 3
1 Jun 3
1 Jul 5
1 Aug 2
1 Sep 2
1 Oct 3
2 Apr 3
2 May 4
2 Jun 2
2 Jul 3
我尝试通过检查先前的值来使用R生成更新的列,但在某些情况下会失败
df$max_value <- df %>% group_by(Id) %>% summarise(max_value = max(value))
df$new_value <- ifelse(lag(df$Id,1) != df$Id, df_$value ,
ifelse(df$value > lag(df$value,1) & lag(df$new_value,1) == df$max_value, df$max_value,
ifelse(lag(df$new_value,1) == df$max_valuue,df$max_value,df$value)))
理解您不能在计算字段时使用相同文件的先前值,我想可以通过为每个ID创建临时值并将其与临时值进行比较来使用for Loop来实现解决方案
expected Result:
ID Month Value New_value
1 Apr 2 2
1 May 3 3
1 Jun 3 3
1 Jul 5 5
1 Aug 2 5
1 Sep 2 5
1 Oct 3 3
2 Apr 3 3
2 May 4 4
2 Jun 2 4
2 Jul 3 4
```
I am very new to programming, any lead to this will be very helpful
Thanks in advance!