如何使用条件语句一步合并具有不同条件的多个数据框
How to merge multiple data frame with different criteria in one step by using condition statement
我目前正在处理下面列出的四个多重数据框。
- tmp:Master table(变量包括:组、重量、国家、收入)
- 价格 1:价格 table 1 组 1 - 3(变量包括:组、重量、价格)
- price2:第 4 - 8 组的价格 table2(变量包括:组、价格)
- 价格 3:第 9 组的价格 table3(变量包括:组、国家、重量、价格)
"weight"是一个从1到200的整型变量,在pricetable1和3中,每个"weight"都分配了一个价格值。
我想保持 master table 不变,并有条件地将价格 table 1,2,3 合并到它。 master table 中的 "group" 变量范围从 1 到 9。价格 table 也是基于 "group" 值进行管理的。每个值代表一种产品。同价位商品table性质相似,价格不同。但是,价格 table 的产品具有不同的性质。由于这种差异,合并标准也不同。
此次合并的目的是比较不同产品的收入与价格。
以下是master table 和3 price table 的合并条件。
- Price table中的主键 1 to master table: group & weight
- Price table 中的主键 2 to master table: group
- 价格中的主键table 3 掌握table: group & country & weight
使用代码:
test <- ifelse(tmp$group %in% c(1,2,3), merge(tmp,price1,by=c("group","weight"))
,ifelse(tmp$group %in% c(4,5,6,7,8),merge(tmp, price2, by= "group")
,merge(tmp,price3, by=c("group","country","weight")))))
我的代码的问题是 return 对象不是数据框,它在 R 中显示为一个大列表。
如果我没理解错的话,你是在master里隔着价格data.frames查商品的价格。由于您的示例中的组是唯一的,因此最好先合并价格 data.frames,然后再与您的主数据合并。
数据:
price1 <- data.frame(group=1:3, weight=sample(1:200, 3, replace=T), price=abs(rnorm(3, 15, 1)))
price2 <- data.frame(group=4:8, price=abs(rnorm(5, 15, 1)))
price3 <- data.frame(group=9, country="A", weight=sample(1:200, 1), price=abs(rnorm(1, 15, 1)))
tmp <- data.frame(group=1:10, weight=sample(1:200, 10, replace=T), country=sample(LETTERS, 10, replace=T), revenue=abs(rnorm(10, 150, 2)))
合并三个价格 data.frame 与 plyr::ldply
library(plyr)
price <- plyr::ldply(list(price1, price2, price3))
合并价格只保留两列data.frame:
library(dplyr)
price <- price %>% select(price, group)
加入大师data.frame以团价
output <- tmp %>% left_join(price, by="group")
group weight country revenue price
1 1 196 N 149.4803 15.52752
2 2 55 L 150.3930 15.98541
3 3 78 E 150.6139 14.95468
4 4 62 D 151.4679 16.51612
5 5 107 Q 149.3292 15.59761
6 6 168 C 148.8256 14.49331
7 7 193 L 149.0341 16.31310
8 8 18 X 152.9192 14.06301
9 9 113 O 147.0069 14.08821
10 10 197 H 149.3581 NA
考虑对子集 tmp 数据帧进行行绑定,每个数据帧都与价格数据帧合并:
test <- rbind(merge(tmp[tmp$group %in% c(1,2,3),], price1, by=c("group","weight")),
merge(tmp[tmp$group %in% c(4,5,6,7,8),], price2, by="group"),
merge(tmp[tmp$group == 9,], price3, by=c("group","country","weight")))
我目前正在处理下面列出的四个多重数据框。
- tmp:Master table(变量包括:组、重量、国家、收入)
- 价格 1:价格 table 1 组 1 - 3(变量包括:组、重量、价格)
- price2:第 4 - 8 组的价格 table2(变量包括:组、价格)
- 价格 3:第 9 组的价格 table3(变量包括:组、国家、重量、价格)
"weight"是一个从1到200的整型变量,在pricetable1和3中,每个"weight"都分配了一个价格值。
我想保持 master table 不变,并有条件地将价格 table 1,2,3 合并到它。 master table 中的 "group" 变量范围从 1 到 9。价格 table 也是基于 "group" 值进行管理的。每个值代表一种产品。同价位商品table性质相似,价格不同。但是,价格 table 的产品具有不同的性质。由于这种差异,合并标准也不同。
此次合并的目的是比较不同产品的收入与价格。
以下是master table 和3 price table 的合并条件。
- Price table中的主键 1 to master table: group & weight
- Price table 中的主键 2 to master table: group
- 价格中的主键table 3 掌握table: group & country & weight
使用代码:
test <- ifelse(tmp$group %in% c(1,2,3), merge(tmp,price1,by=c("group","weight"))
,ifelse(tmp$group %in% c(4,5,6,7,8),merge(tmp, price2, by= "group")
,merge(tmp,price3, by=c("group","country","weight")))))
我的代码的问题是 return 对象不是数据框,它在 R 中显示为一个大列表。
如果我没理解错的话,你是在master里隔着价格data.frames查商品的价格。由于您的示例中的组是唯一的,因此最好先合并价格 data.frames,然后再与您的主数据合并。
数据:
price1 <- data.frame(group=1:3, weight=sample(1:200, 3, replace=T), price=abs(rnorm(3, 15, 1)))
price2 <- data.frame(group=4:8, price=abs(rnorm(5, 15, 1)))
price3 <- data.frame(group=9, country="A", weight=sample(1:200, 1), price=abs(rnorm(1, 15, 1)))
tmp <- data.frame(group=1:10, weight=sample(1:200, 10, replace=T), country=sample(LETTERS, 10, replace=T), revenue=abs(rnorm(10, 150, 2)))
合并三个价格 data.frame 与 plyr::ldply
library(plyr)
price <- plyr::ldply(list(price1, price2, price3))
合并价格只保留两列data.frame:
library(dplyr)
price <- price %>% select(price, group)
加入大师data.frame以团价
output <- tmp %>% left_join(price, by="group")
group weight country revenue price
1 1 196 N 149.4803 15.52752
2 2 55 L 150.3930 15.98541
3 3 78 E 150.6139 14.95468
4 4 62 D 151.4679 16.51612
5 5 107 Q 149.3292 15.59761
6 6 168 C 148.8256 14.49331
7 7 193 L 149.0341 16.31310
8 8 18 X 152.9192 14.06301
9 9 113 O 147.0069 14.08821
10 10 197 H 149.3581 NA
考虑对子集 tmp 数据帧进行行绑定,每个数据帧都与价格数据帧合并:
test <- rbind(merge(tmp[tmp$group %in% c(1,2,3),], price1, by=c("group","weight")),
merge(tmp[tmp$group %in% c(4,5,6,7,8),], price2, by="group"),
merge(tmp[tmp$group == 9,], price3, by=c("group","country","weight")))