我正在寻找一种将销售ID连接到一列销售项目上的方法。
#4 items that are for sale
SaleItems<-data.frame(Appliance=c("Radio", "Laptop", "TV", "Fridge"))
#sales IDs for these 4 items
SalesIDs<-data.frame(Appliance=rep(c("Radio", "Laptop", "TV", "Fridge"), times=c(5,5,5,5)), ID=c(120:139))
我想要的结果(出于演示目的)是按照以下示例将销售ID连接到SaleItems $ Appliance列(在方括号之间):
SaleItemsIDs<-data.frame(Appliance=c("Radio (120, 121, 122, 123, 124)", "Laptop (125, 126, 127, 128, 129)", "TV (130,131,132,133,134)", "Fridge (135, 136, 137, 138, 139)"))
有人可以帮忙吗?
答案 0 :(得分:1)
我们首先可以将“设备”和paste
“ ID”分组在一起,然后paste
“设备”以创建单个字符串
library(dplyr)
SalesIDs %>%
group_by(Appliance) %>%
summarise(ID = toString(ID)) %>%
transmute(Appliance = paste(Appliance, " (", ID, ")", sep=""))