Clay
Clay

Reputation: 2726

dplyr mutate using value from a subset of separate data frame

Given the following two example data frames:

set.seed(2299)
df1 <- data.frame(site = c("site1","site1","site1","site2","site2","site2"),
                  A0 = rnorm(6),B0 = rnorm(6))
df2 <- data.frame(site = c("site1", "site2"),
                  A0dir = c(220, 110), B0dir = c(310,200))

How can I use the A0dir value from df2 where the df2$site == df1$site in the row I am mutating with dplyr?

The following does not work.

df1 %>% 
      mutate(disp = df2$A0dir[site == df2$site] + A0/B0)

The following gets the right answer for "site1", but I need it to work for all sites.

df1 %>% 
      mutate(disp = df2$A0dir["site1" == df2$site] + A0/B0)

Both the following produces Error: not compatible with STRSXP

df1 %>% 
      mutate(disp = subset(df2, site == .$site, select = A0dir))    

df1 %>% 
      mutate(disp = subset(df2, site == "site1", select = A0dir))

In reality, df1 is much larger and I am trying to avoid completing a join to bring in the A0dir and B0dir variables from df2 into df1.

Upvotes: 0

Views: 440

Answers (1)

mtoto
mtoto

Reputation: 24178

We could use match() inside mutate().

df1 %>% mutate(disp = df2$A0dir[match(site, df2$site)] + A0/B0)
#   site         A0         B0     disp
#1 site1 -1.5784780  0.1712790 210.7842
#2 site1  1.0957047  0.6394951 221.7134
#3 site1 -1.3443118  0.1814845 212.5927
#4 site2  1.0674512  0.7809774 111.3668
#5 site2  1.3821173  1.7001376 110.8129
#6 site2 -0.1283199 -0.7093244 110.1809

Upvotes: 1

Related Questions