заполнить переменную 3 возможными строками, основанными на нескольких наблюдениях от нескольких переменных - PullRequest
0 голосов
/ 07 января 2019

Я пытаюсь добиться этого, используя следующие данные и код:

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))

Я хочу заполнить последние две переменные (nonComp и nonCompLease) в зависимости от 'diveLocation' (если diveLocation = 'Compliance', затем эти строки и аналогично, если diveLocation = 'Lease', затем эти строки) и наблюдения других переменных. Я попробовал следующий код:

#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 («да», «да», «да», «да», «да», «да», «FALSE»)) тогда как должно быть: nonComp = c («да», «нет», «нет», NA, NA, NA, NA) nonCompLease = c (NA, NA, NA, «нет», «нет», «да», «да»))

Любая помощь с кодированием для получения желаемого результата будет высоко ценится

1 Ответ

0 голосов
/ 07 января 2019

Пересмотренный код, который показывает, что вы хотите:

    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  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...