r - 合并两个不同大小的数据集,有条件地将同一行从较小的数据集复制到较大数据集中的几行

标签 r merge conditional

我是 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 中的新列)。 Comp(公司)需要始终匹配,但匹配年份以月份为条件:如果月份> 6则年份在数据集之间匹配,如果月份<7则年份+ 1(在df_m中)与年份(在df_f中匹配).注意df_f中的某一行要根据条件放到df_m中的几行中。

想要的输出阐明了问题和目标:

想要的输出:

    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

关于r - 合并两个不同大小的数据集,有条件地将同一行从较小的数据集复制到较大数据集中的几行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34816100/

相关文章:

r - 在函数内使用 sfApply 时出现范围问题(包 Snowfall - R)

舍入数字并强制 R 显示 0

r - 根据日期映射 tibble,具体取决于日期范围

javascript - jQuery 合并多个 javascript 数组值

algorithm - 使用二进制搜索的并行合并排序

Junit:忽略或跳过整个测试类

r - 如何使用 knitr/Sweave 中的 R 变量值在 LaTeX 中编写 if-then 语句

GIT如何将master merge 到用-single-branch克隆的分支

go - 如何在 Go 中的 if 语句中更新变量的值?