在 R 中调整组内的先前值
我正在尝试编写一段代码,允许我根据组 name
的值创建 TRUE
或 FALSE
变量以下 data.frame
弹出的列的最早记录:
library(tidyverse)
name<-c("AAA","AAA","AAA","AAA","AAA","AAA","AAA")
poped<-c(NA,1,NA,NA,1,NA,NA)
order<-c(1:7)
tag<-c("X","Y","X","X","Y","X","X")
> df
name order tag poped
1 AAA 1 X NA
2 AAA 2 Y 1
3 AAA 3 X NA
4 AAA 4 X NA
5 AAA 5 Y 1
6 AAA 6 X NA
7 AAA 7 X NA
我想改变两个名为 CHECK
和 POS
CHECK 的新变量
将采用这些值
1= If the closest (above) value where the tag column is Y and poped is 1
0= If the closest (above) value where the tag column is Y and poped is 0
2 = If the current row has tag = Y
NA = Otherwise
POS
将采用最接近(上方)行号的值,其中标记列为 Y 并且 poped 为 1,否则采用 NA
。
我想要的输出是:
> df
name order tag poped CHECK POS why
1 AAA 1 X NA NA NA There is no previous data
2 AAA 2 Y 1 NA NA current tag = Y
3 AAA 3 X NA 1 2 the closest value above where tag=Y is in row 2 and poped is 1
4 AAA 4 X NA 1 2 the closest value above where tag=Y is in row 2 and poped is 1
5 AAA 5 Y 1 NA NA current tag = Y
6 AAA 6 X NA 1 5 the closest value above where tag=Y is in row 5 and poped is 1
7 AAA 7 X NA 1 5 the closest value above where tag=Y is in row 5 and poped is 1
如何创建一个解决方案,最好使用 Tidyverse?
I'm trying to write a code that will allow me to create a TRUE
or FALSE
variable within the groups name
depending on the value of the earliest record of the column poped of the following data.frame
:
library(tidyverse)
name<-c("AAA","AAA","AAA","AAA","AAA","AAA","AAA")
poped<-c(NA,1,NA,NA,1,NA,NA)
order<-c(1:7)
tag<-c("X","Y","X","X","Y","X","X")
> df
name order tag poped
1 AAA 1 X NA
2 AAA 2 Y 1
3 AAA 3 X NA
4 AAA 4 X NA
5 AAA 5 Y 1
6 AAA 6 X NA
7 AAA 7 X NA
I want to mutate a two new variable named CHECK
and POS
CHECK
will take on the values
1= If the closest (above) value where the tag column is Y and poped is 1
0= If the closest (above) value where the tag column is Y and poped is 0
2 = If the current row has tag = Y
NA = Otherwise
POS
will take on the value of the closest (above) row number where the tag column is Y and poped is 1, and NA
otherwise.
My desired output will be:
> df
name order tag poped CHECK POS why
1 AAA 1 X NA NA NA There is no previous data
2 AAA 2 Y 1 NA NA current tag = Y
3 AAA 3 X NA 1 2 the closest value above where tag=Y is in row 2 and poped is 1
4 AAA 4 X NA 1 2 the closest value above where tag=Y is in row 2 and poped is 1
5 AAA 5 Y 1 NA NA current tag = Y
6 AAA 6 X NA 1 5 the closest value above where tag=Y is in row 5 and poped is 1
7 AAA 7 X NA 1 5 the closest value above where tag=Y is in row 5 and poped is 1
How can I create a solution, ideally using Tidyverse?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出:
Output: