简体   繁体   English

如何根据“NA”是否出现在 R 中的那一行来重新编码新变量?

[英]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.本质上,我试图创建一个变量来区分“NA”是否出现在我的数据集中的多行中。 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".如果 NA 没有出现在行中,我想让“X0”成为“NA”,如果出现,我希望它是一个值,比如“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.如果审查确实发生,NA 已经在 X1:X4 的某处,但如果没有发生,我希望 X0 为 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.我尝试使用此代码(并尝试使用各种变体)但它似乎遗漏了一些代码行,其中包含 NA 的代码行作为 X0 中的 NA。

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.if_any条件下使用case_when我们可以解决这个问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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