如何使用另一个数据框中字段的值调用包含列的值

时间:2018-09-22 21:59:12

标签: r dplyr

我正在尝试将每年的值从df_2移到df_1

countries <- c("USA","Canada","Denmark","Albania", "Turkey","France", "Italy")
values <- c(100, 1000, 300, 300,7000, 8343,9211)
year <- c(2007,2008,2009,2008,2008,2009,2008)
df_1 <- data.frame(countries, values, stringsAsFactors = FALSE)

countries <- c("USA","Canada","Denmark","Albania", "Turkey","France", "Italy")
`2007` <- c(2,3,5,6,7,8,8)
`2008` <- c(23,23,65,86,97,98,68)
`2009` <- c(12,33,35,46,67,78,88)

df_2 <- data.frame(countries, `2007`, `2008`, `2009`, stringsAsFactors = FALSE)

关于如何解决此问题的想法:

df_1 <- dplyr::mutate(df_1, ifelse((df_1$country == df_2$country), df_2$(df_1$year), 0)

预期结果是这是一个新列,其中包含每年和国家/地区的值:

df_1$value_that_year <- c(2,23,35,86,97,78,68)

1 个答案:

答案 0 :(得分:0)

我认为您忘记了在year中包含df_1列,这很遗憾使您的示例无法重现。

让我们在year上添加一个df_1列,据此我们从df_2中选择条目。

countries <- c("USA","Canada","Denmark","Albania", "Turkey","France", "Italy")
values <- c(100, 1000, 300, 300,7000, 8343,9211)
year <- c(2007,2008,2009,2008,2008,2009,2008)
df_1 <- data.frame(countries, values, year, stringsAsFactors = FALSE)

然后,这变得很简单,只需将df_2从宽扩展到长,然后使用right_joindata.frame上的两个year连接在一起。

library(tidyverse)
df_2 %>%
    gather(year, value_that_year, -countries) %>%
    mutate(year = as.numeric(sub("X", "", year))) %>%
    right_join(df_1)
#  countries year value_that_year values
#1       USA 2007               2    100
#2    Canada 2008              23   1000
#3   Denmark 2009              35    300
#4   Albania 2008              86    300
#5    Turkey 2008              97   7000
#6    France 2009              78   8343
#7     Italy 2008              68   9211