我对R完全陌生,我花了一段时间尝试搜索一种代表性的解决方案,但到目前为止还没有找到合适的答案,因此我希望寻求帮助可以在这里解决该问题。

我应该合并两个不同大小的数据集(其他包括年度数据:df_f,以及其他每月数据:df_m)。我应该将较小的df_f合并到较大的df_m,以使df_f的行与df_m有条件地合并。

这是我的问题的描述性示例(带有一些非常基本的可重现数字):

第一个数据集

a <- c(1990)
b <- c(1980:1981)
c <- c(1994:1995)

aa <- rep("A", 1)
bb <- rep("B", 2)
cc <- rep("C", 2)

df1 <- data.frame(comp=factor(c(aa, bb, cc)))
df2 <- data.frame(year=factor(c(a, b, c)))
other.columns <- rep("other_columns", length(df1))

df_f <- cbind(df1, df2, other.columns ) # first dataset


第二数据集

z <- c(10:12)
x <- c(7:12)
xx <- c(1:9)
v <- c(2:9)

w <- rep(1990, length(z))
e <- rep(1980, length(x))
ee <- rep (1981, length(xx))
r <- rep(1995, length(v))

t <- rep("A", length(z))
y <- rep("B", length(x) + length(xx))
u <- rep("C", length(v))

df3 <- data.frame(month=factor(c(z, x, xx, v)))
df4 <- data.frame(year=factor(c(w, e, ee, r)))
df5 <- data.frame(comp=factor(c(t, y, u)))

df_m <- cbind(df5, df4, df3) # second dataset


输出:

> df_m
   comp year month
1     A 1990    10
2     A 1990    11
3     A 1990    12
4     B 1980     7
5     B 1980     8
6     B 1980     9
7     B 1980    10
8     B 1980    11
9     B 1980    12
10    B 1981     1
11    B 1981     2
12    B 1981     3
13    B 1981     4
14    B 1981     5
15    B 1981     6
16    B 1981     7
17    B 1981     8
18    B 1981     9
19    C 1995     2
20    C 1995     3
21    C 1995     4
22    C 1995     5
23    C 1995     6
24    C 1995     7
25    C 1995     8
26    C 1995     9
> df_f
  comp year other.columns
1    A 1990 other_columns
2    B 1980 other_columns
3    B 1981 other_columns
4    C 1994 other_columns
5    C 1995 other_columns


我想根据条件comp,年份和月份将df_f中的行放置到df_m(将数据从df_f存储到df_m中的新列)。公司(公司)必须始终匹配,但是匹配年份取决于月份:如果月份> 6,则数据集之间匹配年份,如果月份
所需的输出阐明了问题和目标:

想要的输出:

    comp year month comp year other.columns
1     A 1990    10    A 1990 other_columns
2     A 1990    11    A 1990 other_columns
3     A 1990    12    A 1990 other_columns
4     B 1980     7    B 1980 other_columns
5     B 1980     8    B 1980 other_columns
6     B 1980     9    B 1980 other_columns
7     B 1980    10    B 1980 other_columns
8     B 1980    11    B 1980 other_columns
9     B 1980    12    B 1980 other_columns
10    B 1981     1    B 1980 other_columns
11    B 1981     2    B 1980 other_columns
12    B 1981     3    B 1980 other_columns
13    B 1981     4    B 1980 other_columns
14    B 1981     5    B 1980 other_columns
15    B 1981     6    B 1980 other_columns
16    B 1981     7    B 1981 other_columns
17    B 1981     8    B 1981 other_columns
18    B 1981     9    B 1981 other_columns
19    C 1995     2    C 1994 other_columns
20    C 1995     3    C 1994 other_columns
21    C 1995     4    C 1994 other_columns
22    C 1995     5    C 1994 other_columns
23    C 1995     6    C 1994 other_columns
24    C 1995     7    C 1995 other_columns
25    C 1995     8    C 1995 other_columns
26    C 1995     9    C 1995 other_columns


提前非常感谢您!我希望这个问题很清楚,至少很难解释。

最佳答案

解决您的问题的基本思路是在年份中添加一个额外的列,以用于匹配。我将使用包dpylr进行此操作和其他操作步骤。

在合并表之前,必须将数字列转换为数字:

library(dplyr)
df_m <- mutate(df_m, year = as.numeric(as.character(year)),
                     month = as.numeric(as.character(month)))
df_f <- mutate(df_f, year = as.numeric(as.character(year)))


原因是您希望能够与月份(month > 6)进行数值比较,并从年份中减去1。您无法做到这一点。

然后我添加用于匹配的列:

df_m <- mutate(df_m, match_year = ifelse(month >= 7, year, year - 1))


在最后一步中,我加入了两个表:

df_new <- left_join(df_m, df_f, by = c("comp", "match_year" = "year"))


参数by确定两个数据帧的哪些列应匹配。输出与您的结果一致:

##    comp year month match_year other.columns
## 1     A 1990    10       1990 other_columns
## 2     A 1990    11       1990 other_columns
## 3     A 1990    12       1990 other_columns
## 4     B 1980     7       1980 other_columns
## 5     B 1980     8       1980 other_columns
## 6     B 1980     9       1980 other_columns
## 7     B 1980    10       1980 other_columns
## 8     B 1980    11       1980 other_columns
## 9     B 1980    12       1980 other_columns
## 10    B 1981     1       1980 other_columns
## 11    B 1981     2       1980 other_columns
## 12    B 1981     3       1980 other_columns
## 13    B 1981     4       1980 other_columns
## 14    B 1981     5       1980 other_columns
## 15    B 1981     6       1980 other_columns
## 16    B 1981     7       1981 other_columns
## 17    B 1981     8       1981 other_columns
## 18    B 1981     9       1981 other_columns
## 19    C 1995     2       1994 other_columns
## 20    C 1995     3       1994 other_columns
## 21    C 1995     4       1994 other_columns
## 22    C 1995     5       1994 other_columns
## 23    C 1995     6       1994 other_columns
## 24    C 1995     7       1995 other_columns
## 25    C 1995     8       1995 other_columns
## 26    C 1995     9       1995 other_columns

10-08 08:31
查看更多