从上面链接中的数据我想转此:
Wood Tabletops RE170590
Wood Tabletops RE170590
Wood Tabletops RE170590
Wood Tabletops RE170590
Wood Tabletops RE170590
Wood Tabletops RE170590
Watertap RE170584
Water Heater RE170584
进入这个:
Wood Tabletops, Wood Tabletops, Wood Tabletops, Wood Tabletops, Wood Tabletops
Watertap, Water Heater
答案 0 :(得分:0)
假设您已将Google电子表格中的表格导入R
作为名为df
的数据框:
library(data.table)
setDT(df)
df2 <- df[, paste(pr, collapse = ", "), by = pr_id]
这使用data.table
按ID编号分组,然后粘贴产品名称。也可以使用其他包和方法。
# Example
df2[pr_id == "RE170593"]
pr_id V1
1: RE170593 Walnut Table tops, Modification of metal table stands reduce height
假设您还没有导入电子表格,那么您可以使用googlesheets
包来使用以下内容直接从Google表格中获取
:
library(googlesheets)
url <- paste0("https://docs.google.com/spreadsheets/d/",
"13RSMGJMWx1mUcIzzj2gVn2N8GHTDTp7aDQTVGMVz0Zw")
gs_obj <- gs_url(url)
df <- setDT(gs_read(gs_obj))
df <- df[complete.cases(df)]
names(df) <- c("pr", "pr_id")
答案 1 :(得分:0)
library(tidyverse)
tab <- tribble(
~ type, ~id,
"Wood.Tabletops" ,"RE170590",
"Wood Tabletops", "RE170590",
"Wood Tabletops", "RE170590",
"Wood Tabletops", "RE170590",
"Wood Tabletops", "RE170590",
"Wood Tabletops", "RE170590",
"Watertap", "RE170584",
"Water Heater", "RE170584"
)
你可以重塑为具有na值的data.frame
tab %>%
rowid_to_column() %>%
spread(rowid, type)
#> # A tibble: 2 x 9
#> id `1` `2` `3` `4` `5` `6` `7` `8`
#> * <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 RE170584 <NA> <NA> <NA> <NA> <NA> <NA> Wate~ Wate~
#> 2 RE170590 Wood.Tabletops Wood T~ Wood T~ Wood ~ Wood ~ Wood ~ <NA> <NA>
您也可以使用list-column
tab %>%
nest(-id) %>%
mutate(data = map(data, pull, type)) %>%
str()
#> Classes 'tbl_df', 'tbl' and 'data.frame': 2 obs. of 2 variables:
#> $ id : chr "RE170590" "RE170584"
#> $ data:List of 2
#> ..$ : chr "Wood.Tabletops" "Wood Tabletops" "Wood Tabletops" "Wood Tabletops" ...
#> ..$ : chr "Watertap" "Water Heater"
或使用某种列表格式
tab %>%
transpose() %>%
rlist::list.group(id) %>%
modify_depth(2, "type") %>%
map(flatten_chr)
#> $RE170584
#> [1] "Watertap" "Water Heater"
#>
#> $RE170590
#> [1] "Wood.Tabletops" "Wood Tabletops" "Wood Tabletops" "Wood Tabletops"
#> [5] "Wood Tabletops" "Wood Tabletops"
由reprex package(v0.1.1.9000)于2018-01-13创建。
答案 2 :(得分:0)
希望这有帮助!
library(dplyr)
library(tidyr)
final_val <- df %>%
group_by(V2) %>%
summarise(val=paste(V1, collapse=", "))
final_val$val
输出是:
[1] " Watertap, Water Heater"
[2] "Wood Tabletops, Wood Tabletops, Wood Tabletops, Wood Tabletops, Wood Tabletops, Wood Tabletops"
#sample data
> dput(df)
structure(list(V1 = c("Wood Tabletops", "Wood Tabletops", " Wood Tabletops",
" Wood Tabletops", " Wood Tabletops",
" Wood Tabletops", " Watertap",
" Water Heater"), V2 = c(" RE170590", " RE170590",
" RE170590", " RE170590", " RE170590", " RE170590", " RE170584",
" RE170584")), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA,
-8L))