根据对多个变量的多次观察,使用3个可能的字符串填充变量

时间:2019-01-07 01:01:55

标签: r dplyr

我正在尝试使用以下数据和代码来实现这一目标:

beg.new <-c(1,  0, 0,   0,  2,  3,  3)
GasBubbles<-c(0,    0,  0,  0,  0,  1,  2)
PF<-    c(0,    0,  0,  1,  1,  0,  0)
debris<-c(0, 1, 0,  0,  0,  1,  0)
diveLocation<-c('Compliance',   'Compliance',   'Compliance',   'Lease',     
'Lease',    'Lease',    'Lease')
nonComp<-   NA
nonCompLease<-  NA

df=data.frame(beg.new,  GasBubbles, PF, debris,     diveLocation,   nonComp,     
nonCompLease)

提供数据框:

structure(list(beg.new = c(1, 0, 0, 0, 2, 3, 3), GasBubbles = c(0, 
0, 0, 0, 0, 1, 2), PF = c(0, 0, 0, 1, 1, 0, 0), debris = c(0, 
1, 0, 0, 0, 1, 0), diveLocation = structure(c(1L, 1L, 1L, 2L, 
2L, 2L, 2L), .Label = c("Compliance", "Lease"), class = "factor"), 
nonComp = c(NA, NA, NA, NA, NA, NA, NA), nonCompLease = c(NA, 
NA, NA, NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, 
-7L))

我想根据“ diveLocation”(如果diveLocation =“ Compliance”,则这些行,以及类似地,如果diveLocation =“ Lease”,则那些行)和其他变量的观察值填充最后两个变量(nonComp和nonCompLease)。我已经尝试了以下代码:

#first noncompliance where diveLocation=='Compliance'
df$nonComp <- if(df$diveLocation=='Compliance' & df$beg.new==1& 
df$beg.new==2& df$beg.new==3& df$GasBubbles==1& df$GasBubbles==2& df$PF==1& 
df$PF==2& df$PF==3){
   print('yes')
}else{
  print('no')
}

#2nd noncompliance where diveLocation=='Lease'
df$nonCompLease <- ifelse(df$diveLocation=='Lease'& df$beg.new==3  & 
df$GasBubbles==2, df$PF==3, 'yes')

不幸的是,我得到了: nonComp = c(“否”,“否”,“否”,“否”,“否”,“否”,“否”) nonCompLease = c(“是”,“是”,“是”,“是”,“是”,“是”,“假”)) 而应该是: nonComp = c(“是”,“否”,“否”,NA,NA,NA,NA) nonCompLease = c(NA,NA,NA,“否”,“否”,“是”,“是”))

对于通过编码获得所需结果的任何帮助,我们将不胜感激

1 个答案:

答案 0 :(得分:0)

修改后的代码显示您想要的代码

    library(tidyverse)
df2 <- as_tibble(df)

df3 <- df2 %>% 
  mutate(nonComp = case_when(diveLocation == "Compliance" & (beg.new %in% c(1, 2, 3) | GasBubbles == 2 | PF %in% c(1, 2, 3)) ~ "Yes",
                             diveLocation == "Lease" ~ NA_character_,
                             TRUE ~ "No")) %>% 
  mutate(nonCompLease = case_when(diveLocation == "Lease" & (beg.new == 3 | GasBubbles == 2 | PF == 3) ~ "Yes",
                                  diveLocation == "Compliance" ~ NA_character_,
                                  TRUE ~ "No"))

df3是:

# A tibble: 7 x 7
  beg.new GasBubbles    PF debris diveLocation nonComp nonCompLease
    <dbl>      <dbl> <dbl>  <dbl> <fct>        <chr>   <chr>       
1       1          0     0      0 Compliance   Yes     NA          
2       0          0     0      1 Compliance   No      NA          
3       0          0     0      0 Compliance   No      NA          
4       0          0     1      0 Lease        NA      No          
5       2          0     1      0 Lease        NA      No          
6       3          1     0      1 Lease        NA      Yes         
7       3          2     0      0 Lease        NA      Yes  
相关问题