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
  • 377,200
  • 20
  • 156
  • 213
Dario Federici
  • 1,228
  • 2
  • 18
  • 40
  • Does this work: `temp$result <- ifelse(is.na(temp$PID_Gen), as.character(temp$PID), as.character(temp$PID_Gen))` – morgan121 Feb 04 '19 at 01: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)

Dan
  • 11,370
  • 4
  • 43
  • 68
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
  • 2,213
  • 1
  • 15
  • 33