简体   繁体   中英

Conditional replacement of values in R

I have a question in R. I have a dataset whose cells I would like to change based on the value of the column next to each other

Data <- tibble(a = 1:5,  
b = c("G","H","I","J","K"),
c = c("G","H","J","I","J"))

I would like to change the chr. to NA if b and c have the same chr.

Desired output

Data <- tibble(a = 1:5,  
    b = c("NA","NA","I","J","K"),
    c = c("NA","NA","J","I","J"))

Thanks a lot for your help in advance.

library(data.table)
setDT(Data)[b == c, c("b", "c") := NA]
#    a    b    c
# 1: 1 <NA> <NA>
# 2: 2 <NA> <NA>
# 3: 3    I    J
# 4: 4    J    I
# 5: 5    K    J

With base R:

Data[Data$b == Data$c, c('b', 'c')] <- "NA"
Data
# # A tibble: 5 x 3
#       a b     c    
# <int> <chr> <chr>
# 1     1 NA    NA   
# 2     2 NA    NA   
# 3     3 I     J    
# 4     4 J     I    
# 5     5 K     J  

Using which to subset Data on the rows where b and c have the same values:

Data[c("b","c")][which(Data$b == Data$c),] <- NA

Result:

Data
# A tibble: 5 x 3
      a b     c    
  <int> <chr> <chr>
1     1 NA    NA   
2     2 NA    NA   
3     3 I     J    
4     4 J     I    
5     5 K     J 

With dplyr

library(dplyr)
Data %>%
  rowwise() %>% 
  mutate(b = ifelse(b %in% c & c %in% b, "NA", b))%>%
  mutate(c = ifelse(b == "NA", "NA", c))

Output:

      a b     c    
  <int> <chr> <chr>
1     1 NA    NA   
2     2 NA    NA   
3     3 I     J    
4     4 J     I    
5     5 K     J    

Another base R option

cols <- c("b", "c")
Data[cols] <- replace(Data[cols], Data[cols] == Data[rev(cols)], NA)

gives

> Data
# A tibble: 5 x 3
      a b     c
  <int> <chr> <chr>
1     1 NA    NA
2     2 NA    NA
3     3 I     J
4     4 J     I
5     5 K     J

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