将行值与列名匹配,并提取每列的最后一个值

时间:2017-08-09 09:44:35

标签: r dataframe merge

...使用merge()或任何其他基本R函数。

例如,这里有两个数据框:

    V1   V2    V3
1 DDPC 1747 57.19
2 DDSG 1466 68.16
3  DDG 2312  43.2
4  DDL 2058 48.56
5 DDUR 1779 56.14
6 DDTH 1883 53.06
7 DDAC 2020 49.47

            DDG   DDL   DDO   DDUR   DDAC  DDMG   DDSG  DDTH    DDPC   DDSUP
2014-08-05 56.49 61.96 43.81 46.975 51.070 52.27 36.320    NA 1920.21 1364.44
2014-08-06 56.40 61.88 43.50 46.860 50.850 52.01 36.305    NA 1920.24 1352.94
2014-08-07 55.79 61.53 43.30 46.310 50.880 51.79 36.105    NA 1909.57 1343.21
2014-08-08 56.29 61.71 43.78 46.530 50.800 52.27 36.545    NA 1931.59 1334.40
2014-08-11 56.40 61.95 44.41 46.920 51.190 52.89 36.725    NA 1936.92 1352.42

我希望V4包含与V1行对应的列的最后一个值:

     V1   V2    V3 V4
1 DDPC 1747 57.19 1936.92
2 DDSG 1466 68.16 36.725
3  DDG 2312  43.2 56.40
4  DDL 2058 48.56 61.95
5 DDUR 1779 56.14 46.92
6 DDTH 1883 53.06 NA
7 DDAC 2020 49.47 51.19

查看this但无法正确应用。

修改 - 也有兴趣从V3获取df1列,并在匹配df2 V1的相应列名下进行分配?

2 个答案:

答案 0 :(得分:1)

试试这个 -

dfa <- data.frame(V1 = letters[1:4], V2 = sample(10,4), V3 = sample(10,4))
dfb <- data.frame(a = sample(10,4),b = sample(10,4), c=sample(10,4), d=sample(10,4))
dfa['V4'] = unlist(sapply(dfa['V1'],function(x){dfb[nrow(dfb),x]}))

答案 1 :(得分:1)

> dput(A)
structure(list(V1 = c("DDPC", "DDSG", "DDG", "DDL", "DDUR", "DDTH", 
"DDAC"), V2 = c("1747", "1466", "2312", "2058", "1779", "1883", 
"2020"), V3 = c("57.19", "68.16", "43.2", "48.56", "56.14", "53.06", 
"49.47")), row.names = c(NA, -7L), .Names = c("V1", "V2", 
"V3"), class = "data.frame")

> dput(B)
structure(list(Date = c("2014-08-05", "2014-08-06", "2014-08-07", 
"2014-08-08", "2014-08-11"), DDG = c("56.49", "56.4", "55.79", 
"56.29", "56.4"), DDL = c("61.96", "61.88", "61.53", "61.71", 
"61.95"), DDO = c("43.81", "43.5", "43.3", "43.78", "44.41"), 
    DDUR = c("46.975", "46.86", "46.31", "46.53", "46.92"), DDAC = c  ("51.07", 
"50.85", "50.88", "50.8", "51.19"), DDMG = c("52.27", "52.01", 
"51.79", "52.27", "52.89"), DDSG = c("36.32", "36.305", "36.105", 
"36.545", "36.725"), DDTH = c(NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_), DDPC = c("1920.21", 
"1920.24", "1909.57", "1931.59", "1936.92"), DDSUP = c("1364.44", 
"1352.94", "1343.21", "1334.4", "1352.42")), .Names = c("Date", 
"DDG", "DDL", "DDO", "DDUR", "DDAC", "DDMG", "DDSG", "DDTH", 
"DDPC", "DDSUP"), row.names = c(NA, -5L), class = "data.frame")

代码:

  A[,"V4"]=unlist(B[nrow(B),match(A[,1],names(B))])
  A
      V1   V2    V3      V4
  1 DDPC 1747 57.19 1936.92
  2 DDSG 1466 68.16  36.725
  3  DDG 2312  43.2    56.4
  4  DDL 2058 48.56   61.95
  5 DDUR 1779 56.14   46.92
  6 DDTH 1883 53.06    <NA>
  7 DDAC 2020 49.47   51.19