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

前端 未结 5 1280
暗喜
暗喜 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:20

    my solution(which maybe seems awkward)

    for (i in 1:length(df$eth)) {
        df$Inc[i] <- Inc[as.character(df$eth[i])][df$ZIP[i],]
    }
    

提交回复
热议问题