簡體   English   中英

R:將數據框x中的行名與數據框y中的列名匹配,以使兩個數據框共享的列中的給定變量值

[英]R: match row name in data frame x with column name in data frame y for a given variable value in column shared by the two data frames

說我有兩個看起來像下面的數據框

> df1
        date firm1 firm2 firm3
1 01-01-2017     1     2    3
2 01-02-2017     4     5    6
3 01-03-2017     7     8    9

> df2
            date
firm1 01-02-2017
firm2 01-01-2017
firm3 01-03-2017

是否可以使用df1中的值(其中df1的列名和日期值與df2的行名和日期相匹配)提取新的數據幀?

希望獲得的數據幀如下所示:

      Value
firm1     2
firm2     4
firm3     9

任何建議將不勝感激!

我們可以使用row/column索引從'df1'中提取值並創建一個data.frame

df3 <- data.frame(Value = df1[-1][cbind(1:nrow(df1), match(df2$date, df1$date))])
row.names(df3) <- row.names(df2)
df3
#      Value
#firm1     2
#firm2     4
#firm3     9
library(reshape2)
#regenerating the initial datasets
date <- c("01-01-2017","01-02-2017","01-03-2017")
firm1 <- c(1,4,7)
firm2 <- c(2,5,8)
firm3 <- c(3,6,9)
df1 <- data.frame(date,firm1,firm2,firm3)

df1
        date  firm1  firm2  firm3
1: 01-01-2017     1     2     3
2: 01-02-2017     4     5     6
3: 01-03-2017     7     8     9

variable <- c("firm1","firm2","firm3")
date <- c("01-02-2017","01-01-2017","01-03-2017")
df2 <- data.frame(date,variable)

df2
      date      variable
1: 01-01-2017    firm1
2: 01-02-2017    firm2
3: 01-03-2017    firm3

#changing the format from wide to long

df1b <- melt(df1,id.vars = "date")
df1b
       date variable value
1: 01-01-2017    firm1     1
2: 01-02-2017    firm1     4
3: 01-03-2017    firm1     7
4: 01-01-2017    firm2     2
5: 01-02-2017    firm2     5
6: 01-03-2017    firm2     8
7: 01-01-2017    firm3     3
8: 01-02-2017    firm3     6
9: 01-03-2017    firm3     9

res <- merge(df2,df1b,by=c("date","variable"))

res  
    date variable value
1: 01-01-2017    firm2     2
2: 01-02-2017    firm1     4
3: 01-03-2017    firm3     9

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM