Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify data.frame column with data and condition from another data.frame [duplicate]

I have 2 data.frames

df <- data.frame(addr=c('a','b','c','d'), num = c(1,2,3,4))

> df
  addr num
1    a   1
2    b   2
3    c   3
4    d   4

df2 <- data.frame(addr=c('a','b','d'), num=c(100,200,500))

> df2
  addr num
1    a 100
2    b 200
3    d 500

And I would like to replace the values in df$num with the values from df2$num if the condition df$addr matches df2$addr. I managed to get this with the following code

 df[,"num"] <- sapply(df[,"addr"], function(x) ifelse(x %in% df2$addr,df2$num[match(x,df2$addr)],df[df$addr==x,]$num))

> df
  addr num
1    a 100
2    b 200
3    c   3
4    d 500

I was wondering if there is a more elegant method using dplyr or data.table?

like image 715
andrnev Avatar asked Apr 16 '26 18:04

andrnev


1 Answers

You can use a combination of left_join and mutate from dplyr

Edit

library(dplyr)

df3 <- df %>% 
  left_join(df2,  by = "addr") %>% 
  mutate(num = ifelse(.$num.y %in% df2$num, .$num.y, df$num)) %>% 
  select(addr, num)

df3
# addr num
#1    a 100
#2    b 200
#3    c   3
#4    d 500

old answer

 df3 <- df %>% 
      mutate(num = ifelse(addr %in% df2$addr, df2$num, num))

df3
#  addr num
#1    a 100
#2    b 200
#3    c   3
#4    d 100
like image 93
patL Avatar answered Apr 19 '26 08:04

patL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!