通过将列名称与数据框中的变量匹配,将值从数据框中分配到另一个数据框中

时间:2020-07-01 08:57:51

标签: r

我有一个这样的数据框:

data <- data.frame(Time = rep(c("Jan 1999", "Feb 1999", "Mar 1999"), each = 3), Country = rep(c("Australia", "Brazil", "Canada"), 3), rep(Group = c("A", "B", "A"), 3), Intercept = NA)

和另一个具有回归系数的数据框,其中A和B是不同组的截距。

coeffs <- data.frame(Time = c("Jan 1999", "Feb 1999", "Mar 1999"), A = c(1,2,3), B = c(3,2,1))

现在,我想将来自coeffs数据帧的拦截信息放入数据帧的拦截列中。我是通过以下方式做到的:

l <- length(unique(data[,"Country"]))
data[,"Intercept"] <- ifelse(data_1[,"Group_1"] == "A", rep(coeffs_1[,"A"], each = l), rep(coeffs_1[,"B"], each = l))

这似乎对于2个小组效果很好,但是现在我需要对7个小组做同样的事情,而且我看不到如何概括上述方法。我想我可以使用7级嵌套ifelse语句或for循环,但是必须有一种更优雅的方法。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

以长格式获取coeffs并加入data

library(dplyr)

coeffs %>%
  tidyr::pivot_longer(cols = -Time, names_to = 'Group', 
                      values_to = 'Intercept') %>%
  right_join(data, by = c('Time', 'Group'))

# A tibble: 9 x 4
#  Time     Group Intercept Country  
#  <chr>    <chr>     <dbl> <chr>    
#1 Jan 1999 A             1 Australia
#2 Jan 1999 A             1 Canada   
#3 Jan 1999 B             3 Brazil   
#4 Feb 1999 A             2 Australia
#5 Feb 1999 A             2 Canada   
#6 Feb 1999 B             2 Brazil   
#7 Mar 1999 A             3 Australia
#8 Mar 1999 A             3 Canada   
#9 Mar 1999 B             1 Brazil   

将此数据框用于data

data <- data.frame(Time = rep(c("Jan 1999", "Feb 1999", "Mar 1999"), each = 3),
                   Country = rep(c("Australia", "Brazil", "Canada"), 3), 
                   Group = rep(c("A", "B", "A"), 3))