Conditional Replacing with NA in R (two dataframes

2019-07-13 04:44发布

问题:

I have

    idx <- c(1397, 2000, 3409, 3415, 4077, 4445, 5021, 5155) 

    idy <- c( 1397, 2000, 2860, 3029, 3415, 3707, 4077, 4445, 5021, 5155, 
             5251, 5560)

   agex <- c(NA, NA, NA, 35, NA, 62, 35, 46)

   agey <- c( 3, 45,  0, 89,  7,  2, 13, 24, 58,  8,  3, 45)


   dat1 <- as.data.frame(cbind(idx, agex))
   dat2 <- as.data.frame(cbind(idy, agey))

Now I want whenever agex = NA, and idx = idy, that agey = NA, so that

       idy agey
  1    1397   NA
  2    2000   NA
  3    2860    0
  4    3029   89
  5    3415    7
  6    3707    2
  7    4077   NA
  8    4445   24
  9    5021   58
  10   5155    8
  11   5251    3
  12   5560   45

I have tried this

ifelse(is.na(dat1$agex) | dat1$idx %in% dat2$idy, NA, dat2$agey)

it returns NAs at the correct indices, but shortens idy to the length of idx.

回答1:

I want whenever agex = NA, and idx = idy, that agey = NA

With a data.table update join...

library(data.table)
setDT(dat1); setDT(dat2)

dat2[dat1[is.na(agex)], on=.(idy = idx), agey := NA]

dat2

     idy agey
 1: 1397   NA
 2: 2000   NA
 3: 2860    0
 4: 3029   89
 5: 3415    7
 6: 3707    2
 7: 4077   NA
 8: 4445   24
 9: 5021   58
10: 5155    8
11: 5251    3
12: 5560   45

How it works

  • dat1[is.na(agex)] is the subset where agex is NA
  • DT[mDT, on=, j] is a join where rows of mDT are looked up in DT using on=
  • j is done in the joined subset of DT
  • when j is k := expr, column k of DT is updated