调节 R 中组内的先前值
Conditioning previous values within groups in 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 的问题,我将不胜感激,但我会接受所有可能的解决方案,非常感谢
df %>%
mutate(ctag=if_else(tag=="Y",tag,as.character(NA)),
cpop=if_else(tag=="Y",poped,as.double(NA)),
maxr=if_else(tag=="Y" & poped==1,order,as.integer(NA))) %>%
fill(ctag,cpop,maxr) %>%
mutate(
CHECK = case_when(
tag == "Y"~2,
lag(ctag) == "Y" & lag(cpop)==1 ~1,
lag(ctag) == "Y" & lag(cpop)==0 ~0,
TRUE~as.double(NA)),
POS = if_else(tag=="Y", as.integer(NA), maxr)
) %>%
select(!ctag:maxr)
输出:
name order tag poped CHECK POS
<chr> <int> <chr> <dbl> <dbl> <int>
1 AAA 1 X NA NA NA
2 AAA 2 Y 1 2 NA
3 AAA 3 X NA 1 2
4 AAA 4 X NA 1 2
5 AAA 5 Y 1 2 NA
6 AAA 6 X NA 1 5
7 AAA 7 X NA 1 5
我正在尝试编写一个代码,允许我根据最早记录的值在组 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 的问题,我将不胜感激,但我会接受所有可能的解决方案,非常感谢
df %>%
mutate(ctag=if_else(tag=="Y",tag,as.character(NA)),
cpop=if_else(tag=="Y",poped,as.double(NA)),
maxr=if_else(tag=="Y" & poped==1,order,as.integer(NA))) %>%
fill(ctag,cpop,maxr) %>%
mutate(
CHECK = case_when(
tag == "Y"~2,
lag(ctag) == "Y" & lag(cpop)==1 ~1,
lag(ctag) == "Y" & lag(cpop)==0 ~0,
TRUE~as.double(NA)),
POS = if_else(tag=="Y", as.integer(NA), maxr)
) %>%
select(!ctag:maxr)
输出:
name order tag poped CHECK POS
<chr> <int> <chr> <dbl> <dbl> <int>
1 AAA 1 X NA NA NA
2 AAA 2 Y 1 2 NA
3 AAA 3 X NA 1 2
4 AAA 4 X NA 1 2
5 AAA 5 Y 1 2 NA
6 AAA 6 X NA 1 5
7 AAA 7 X NA 1 5