0

I've got a data.table named temp:

PID PID_Gen 
a   NA   
c   c1
b   NA
d   d1

The following evaluates if PID_Gen is NULL and in that case assigns the value of PID.

temp$result <- future_sapply(temp$PID_Gen, 
                             FUN = function(x) if(is.na(x)) {temp$PID})

The problem is that it overflows the memory.

Are there any alternatives I can use?

Ronak Shah
  • 286,338
  • 16
  • 97
  • 143
Dario Federici
  • 828
  • 2
  • 13
  • 29

2 Answers2

2

Is this your desired result?

df <- read.table(text = "PID PID_Gen 
a   NA   
c   c1
b   NA
d   d1", header = TRUE, stringsAsFactors = FALSE)

df$PID_Gen[is.na(df$PID_Gen)] <- df$PID[is.na(df$PID_Gen)]

print(df)
#>   PID PID_Gen
#> 1   a       a
#> 2   c      c1
#> 3   b       b
#> 4   d      d1

Created on 2019-02-03 by the reprex package (v0.2.1.9000)

Lyngbakr
  • 9,482
  • 1
  • 29
  • 49
2

Here's my approach:

temp <- read.table(text = "PID PID_Gen 
                   a   NA   
                   c   c1
                   b   NA
                   d   d1", header = TRUE, stringsAsFactors = FALSE)
temp$result <- ifelse(is.na(temp$PID_Gen), temp$PID, temp$PID_Gen)
morgan121
  • 1,989
  • 1
  • 12
  • 27