Adrien
Adrien

Reputation: 471

Update columns using other columns of same data.table, using only their string name

I have a lot of columns that I want to update using values of other columns in the same data.table. I need to use their string names. Indeed, I have very large vector of variable names of the form:

cols.x <- c('name1.x', 'name2.x', 'name3.x', ...)

that I want to update using other columns:

cols.y <- c('name1.y', 'name2.y', 'name3.y', ...)

However, I could not find a way to make data.table understand I have also columns in the right hand side of the :=, and not the string.

Here is the working example:

library(data.table)

# Setting the data.table
DT <- data.table(V1.x=c(NA,c(1:5)), V2.x=c(NA,LETTERS[1:5]), V1.y=6:1, V2.y=LETTERS[1:6])

# Preparing the columns
cols.x <- c('V1.x','V2.x')
cols.y <- c('V1.y','V2.y')

# Assign cols.y to cols.x when cols.x is NA
DT[is.na(V1.x) | is.na(V2.x), cols.x := cols.y, with = F]

print(DT)

# Output
    V1.x V2.x V1.y V2.y
1:   NA  V1.y   6    A
2:    1    A    5    B
3:    2    B    4    C
4:    3    C    3    D
5:    4    D    2    E
6:    5    E    1    F

# Desired Output
    V1.x V2.x V1.y V2.y
1:    6    A    6    A
2:    1    A    5    B
3:    2    B    4    C
4:    3    C    3    D
5:    4    D    2    E
6:    5    E    1    F

I tried many other solution such as:

DT[is.na(V1.x) | is.na(V2.x), cols.x := get(cols.y)]
# or
DT[is.na(V1.x) | is.na(V2.x), cols.x := as.list(cols.y)]

but nothing gives me the desired output.

Upvotes: 0

Views: 38

Answers (1)

Adrien
Adrien

Reputation: 471

I just found this solution that works

DT[is.na(V1.x) | is.na(V2.x), (cols.x) := DT[is.na(V1.x) | is.na(V2.x),cols.y, with = F]]

Upvotes: 1

Related Questions