Boost Boost - 1 year ago 72
R Question

R: Pulling values from a second dataframe by indexing with values from another dataframe

For every entry in a column of dataframe #1, I want to see if that value is in a dataframe #2 and then grab a value from a particular column from the second dataframe, else 0 if it can't find it. Is there a way to use one of the *apply functions for this?

df1 <- data.frame(
key1 = c("A","B","C","E")

df2 <- data.frame(
key2 = c("X", "A", "C", "D", "E"),
val2 = as.integer(c('1','2','23','41','99'))

#Answer should be a vector like this:
x <- as.integer(c('2','0','23','99'))

Answer Source

The code below will give your the results in your example, but if the key appears more than once in df2 it will return only the first result. If that is not what you want, please describe the desired output for that scenario.

x <- as.integer(df2[["val2"]][match(df1[["key1"]], df2[["key2"]])])
x[] <- as.integer(0)

match returns the locations of the positions of matches of its first argument in its second. match will return NA for non matches, which will create a NA value when it indexes into df2[["val2"]], so those values have to be changed to 0 to get the final result.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download