Reputation: 401
Consider the following two data.tables:
df1=data.table(a=1:3, b=4:6, c=7:9)
df2=data.table(a=c(T,F,T), c=c(F,F,T), d=c(T,F,F))
What is the best way to update columns a
and c
of df1
with the corresponding values from df2
?
df1[,c("a","c"),with=FALSE]
and df2[,c("a","c"),with=FALSE]
return the corresponding parts of each data.table;
but df1[,c("a","c"),with=FALSE] <- df2[,c("a","c"),with=FALSE]
returns an error!
Upvotes: 0
Views: 48
Reputation: 1263
Here's another solution:
library(tidyverse)
df1 <- tibble(a = 1:3, b = 4:6, c = 7:9)
df2 <- tibble(a = c(T,F,T), c = c(F,F,T), d = c(T,F,F))
bind_cols(df1, df2) %>%
transmute(a = a1, b, c = c1)
This creates a table with all six columns and then the transmute
call selects and renames the ones you're interested in.
Upvotes: 1