Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 按行和列名以及按组合并两个数据帧_R_Merge - Fatal编程技术网

R 按行和列名以及按组合并两个数据帧

R 按行和列名以及按组合并两个数据帧,r,merge,R,Merge,我有两个数据帧,df1和df2,如下所示: df1<- data.frame(year, week, X1, X2) df1 year week X1 X2 1 2010 1 2 3 2 2010 2 8 6 3 2011 1 7 5 firm<-c("X1", "X1", "X2") year <- c(2010,2010,2011) week<- c(1, 2,

我有两个数据帧,df1和df2,如下所示:

 df1<- data.frame(year, week, X1, X2)
 df1
  year week X1 X2
1 2010    1  2  3
2 2010    2  8  6
3 2011    1  7  5
 
 firm<-c("X1", "X1", "X2")
 year <- c(2010,2010,2011)
 week<- c(1, 2, 1)
 cost<-c(10,30,20)
 
 df2<- data.frame(firm,year, week, cost)
 df2
  firm year week cost
1   X1 2010    1   10
2   X1 2010    2   30
3   X2 2011    1   20
df3 
  firm  year week cost Y 
1  X1   2010   1   10  2 
2  X1   2010   2   30  8 
3  X2   2011   1   20  5
其中“Y”是一个新变量,反映df1中特定年份和周的X1和X2值。
有没有办法在R中做到这一点?提前感谢您的回复。

我们可以将第一个数据集重塑为“长”格式,然后与第二个数据集进行连接

library(dplyr)
library(tidyr)
df1 %>% 
 pivot_longer(cols = X1:X2, values_to = 'Y', names_to = 'firm') %>% 
 right_join(df2)
-输出

# A tibble: 3 x 5
#   year  week firm      Y  cost
#  <dbl> <dbl> <chr> <int> <dbl>
#1  2010     1 X1        2    10
#2  2010     2 X1        8    30
#3  2011     1 X2        5    20
#一个tible:3 x 5
#年周固定成本
#      
#1 2010 1 X1 2 10
#2 2010 2 X1 8 30
#2011年3月1日X2 5 20
数据
df1这里有一个基本R选项(从中借用数据,谢谢!)

df1 <- structure(list(year = c(2010L, 2010L, 2011L), week = c(1L, 2L, 
1L), X1 = c(2L, 8L, 7L), X2 = c(3L, 6L, 5L)), class = "data.frame", 
row.names = c("1", 
"2", "3"))

df2 <- structure(list(firm = c("X1", "X1", "X2"), year = c(2010, 2010, 
2011), week = c(1, 2, 1), cost = c(10, 30, 20)), class = "data.frame", 
row.names = c(NA, 
-3L))
q <- startsWith(names(df1),"X")
v <- cbind(df1[!q],stack(df1[q]),row.names = NULL)
df3 <- merge(setNames(v,c(names(df1)[!q],"Y","firm")),df2)
> df3
  year week firm Y cost
1 2010    1   X1 2   10
2 2010    2   X1 8   30
3 2011    1   X2 5   20