簡體   English   中英

如何基於一個數據框中的列的值和R中另一個數據框的列標題名稱有條件地創建新列

[英]how to conditionally create new column based on the values of a column in one dataframe and the column header names of another dataframe in R

我有一個數據averageDate df1 ,該數據averageDate具有一個名為averageDate的列,該列包含日期,格式為%Y-%m。

我有另一個數據df2 ,其中大多數列名稱是%Y-%m格式的日期值,這些列中的數據是經濟指標的數值。

我想在df1中填充新列(在df3 ),其中的值是在df2中找到的值,其中averageDate中的averageDate值與df2中的列名稱匹配。

我已經成功解決了基於單獨數據幀中兩列的條件合並的問題,但是我遇到的問題是第二個匹配條件是df2的列名稱。

我的數據幀的復制如下所示:

df1 <- structure(list(zipcode = structure(c(1L, 2L, 4L, 3L), .Label = c("10019", 
"10027", "20009", "94117"), class = "factor"), averageDate = c("2017-08", 
"2017-04", NA, "2015-11")), .Names = c("zipcode", "averageDate"
), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"
))

df2 <-structure(list(RegionName = c(20009, 10019, 10027), `2015-01` = c(444500, 
1855000, NA), `2015-02` = c(439000, 1715000, NA), `2015-03` = c(437000, 
1775000, NA), `2015-04` = c(475000, 1855000, NA), `2015-05` = c(489000, 
1860000, NA), `2015-06` = c(489750, 1877500, NA), `2015-07` = c(479900, 
1957500, NA), `2015-08` = c(489900, 1950000, NA), `2015-09` = c(5e+05, 
1947500, NA), `2015-10` = c(512450, 1958000, NA), `2015-11` = c(503999.5, 
1990000, NA), `2015-12` = c(499900, 1995000, NA), `2016-01` = c(499500, 
1995000, NA), `2016-02` = c(529900, 1822500, NA), `2016-03` = c(5e+05, 
1820000, 872000), `2016-04` = c(5e+05, 1930000, 887000), `2016-05` = c(492500, 
1795500, 837000), `2016-06` = c(529000, 1750000, 819000), `2016-07` = c(549000, 
1832500, 725800), `2016-08` = c(577000, 1850000, 725000), `2016-09` = c(549900, 
1762500, 753500), `2016-10` = c(529000, 1777500, 737900), `2016-11` = c(519000, 
1787000, 750000), `2016-12` = c(499000, 1795000, 725800), `2017-01` = c(549000, 
1795000, 749000), `2017-02` = c(522450, 1833000, 845000), `2017-03` = c(546950, 
1836500, 867250), `2017-04` = c(572247.5, 1849450, 929000), `2017-05` = c(549900, 
1850000, 929000), `2017-06` = c(540000, 1875000, 899000), `2017-07` = c(519900, 
1895000, 899000), `2017-08` = c(525000, 1849990, 897000), `2017-09` = c(572450, 
1795000, 840000), `2017-10` = c(595000, 1795000, 882000), `2017-11` = c(555650, 
1825000, 949000), `2017-12` = c(525000, 1799950, 795000), `2018-01` = c(557000, 
1925000, 772500)), .Names = c("RegionName", "2015-01", "2015-02", 
"2015-03", "2015-04", "2015-05", "2015-06", "2015-07", "2015-08", 
"2015-09", "2015-10", "2015-11", "2015-12", "2016-01", "2016-02", 
"2016-03", "2016-04", "2016-05", "2016-06", "2016-07", "2016-08", 
"2016-09", "2016-10", "2016-11", "2016-12", "2017-01", "2017-02", 
"2017-03", "2017-04", "2017-05", "2017-06", "2017-07", "2017-08", 
"2017-09", "2017-10", "2017-11", "2017-12", "2018-01"), row.names = c(38L, 
82L, 226L), class = "data.frame")

df3 <- structure(list(RegionName = c("10019", "10027", "20009", "94117"
    ), variable = structure(c(32L, 28L, 11L, NA), .Label = c("2015-01", 
    "2015-02", "2015-03", "2015-04", "2015-05", "2015-06", "2015-07", 
    "2015-08", "2015-09", "2015-10", "2015-11", "2015-12", "2016-01", 
    "2016-02", "2016-03", "2016-04", "2016-05", "2016-06", "2016-07", 
    "2016-08", "2016-09", "2016-10", "2016-11", "2016-12", "2017-01", 
    "2017-02", "2017-03", "2017-04", "2017-05", "2017-06", "2017-07", 
    "2017-08", "2017-09", "2017-10", "2017-11", "2017-12", "2018-01"
    ), class = "factor"), value = c(1849990, 929000, 503999.5, NA
    )), .Names = c("RegionName", "variable", "value"), row.names = c(NA, 
    -4L), class = "data.frame")

像這樣嗎

require(tidyverse);
left_join(
    df1,
    df2 %>%
        gather(averageDate, Value, 2:ncol(df2)) %>%
        rename(zipcode = RegionName) %>%
        mutate(zipcode = as.character(zipcode)))
## A tibble: 4 x 3
#  zipcode averageDate   Value
#  <chr>   <chr>         <dbl>
#1 10019   2017-08     1849990
#2 10027   2017-04      929000
#3 94117   NA               NA
#4 20009   2015-11      504000

來自@Marko的見解

long <-melt(df2, id.vars = "RegionName")

df3 <- merge(long, df1, by.x=c("RegionName", "variable"), by.y=c("zipcode", "averageDate"), all.y=T)

暫無
暫無

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

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