简体   繁体   中英

How to recode a new variable based on whether 'NA' appears across that row in R?

I feel like this may be a very easy fix but I can't seem to get it to work correctly, I'm sorry. Essentially, I am trying to create a variable that dichotomizes whether 'NA' appears across multiple rows within my dataset. So with this data,

id <- c(1:6)
X0 <- NA
X1 <- c(5,NA,7,8,1,5)
X2 <- c(5,0,0,NA,3,7)
X3 <- c(NA,2,3,4,2,7)
X4 <- c(1,1,5,2,1,7)
df <- data.frame(id,X0,X1,X2,X3,X4)


  id X0 X1 X2 X3 X4
1  1 NA  5  5 NA  1
2  2 NA NA  0  2  1
3  3 NA  7  0  3  5
4  4 NA  8 NA  4  2
5  5 NA  1  3  2  1
6  6 NA  5  7  7  7

I'd want to make "X0" to be "NA" if NA does not appear across the rows and if it does, I want it to be a value, let's say "1". Essentially, I am trying to determine whether censoring occurs across that respondent's timepoints. If censoring does occur, NA would already be somewhere across X1:X4 but if it doesn't, I want X0 to be NA. The end result would look like this:

  id X0 X1 X2 X3 X4
1  1  1  5  5 NA  1
2  2  1 NA  0  2  1
3  3 NA  7  0  3  5
4  4  1  8 NA  4  2
5  5 NA  1  3  2  1
6  6 NA  5  7  7  7

I tried using this code (and played around with variations) but it seems to miss a few and code rows that have NA's in them as NA in X0.

df$X0 <-  case_when((is.na(df$X1| df$X2| df$X3)) ~ 1,
                        (!is.na(df$X1| df$X2| df$X3)) ~ NA)

Hopefully that makes sense. Thanks very much in advance.

With if_any in a case_when condition we can solve this.

library(dplyr)

id <- c(1:6)
X0 <- NA
X1 <- c(5, NA, 7, 8, 1, 5)
X2 <- c(5, 0, 0, NA, 3, 7)
X3 <- c(NA, 2, 3, 4, 2, 7)
X4 <- c(1, 1, 5, 2, 1, 7)
df <- data.frame(id, X0, X1, X2, X3, X4)

df |>
  mutate(X0 = case_when(
    if_any(X1:X4, is.na) ~ "1",
    TRUE ~ NA_character_
  ))
#>   id   X0 X1 X2 X3 X4
#> 1  1    1  5  5 NA  1
#> 2  2    1 NA  0  2  1
#> 3  3 <NA>  7  0  3  5
#> 4  4    1  8 NA  4  2
#> 5  5 <NA>  1  3  2  1
#> 6  6 <NA>  5  7  7  7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM