R - Create a new variable where each observation depends on another table and other variables in the data frame

前端 未结 5 1222
暗喜
暗喜 2021-01-18 02:06

I have the two following tables:

df <- data.frame(eth = c(\"A\",\"B\",\"B\",\"A\",\"C\"),ZIP1 = c(1,1,2,3,5))
Inc <- data.frame(ZIP2 = c(1,2,3,4,5,6,7)         


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 02:19

    Another option:

    library(dplyr)
    library(tidyr)
    Inc %>%
      gather(eth, value, -ZIP2) %>%
      left_join(df, ., by = c("eth", "ZIP1" = "ZIP2"))
    

提交回复
热议问题