[英]How to aggregate Character Data of a data frame based on different column in r
我是 R 的新手。 我有一個這樣的數據框:
Category description
Analysis This is one
Hybrid This is two
Other This is three
Analysis This is four
Other This is five
Hybrid This is six
我想以這樣的列表形式獲取每個類別的描述:
Category description
Analysis ("This is one", "This is four")
Hybrid ("This is two", "This is six")
Other ("This is five", "This is three")
這是否有效:
library(dplyr)
df %>% group_by(Category) %>%
mutate(description = str_c('"',description, '"')) %>%
summarise(description = str_c('(',toString(description),')'))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 2
Category description
<chr> <chr>
1 Analysis "(\"This is one\", \"This is four\")"
2 Hybrid "(\"This is two\", \"This is six\")"
3 Other "(\"This is three\", \"This is five\")"
cat(x$description)
("This is one", "This is four") ("This is two", "This is six") ("This is three", "This is five")
使用的數據:
dput(df)
structure(list(Category = c("Analysis", "Hybrid", "Other", "Analysis",
"Other", "Hybrid"), description = c("This is one", "This is two",
"This is three", "This is four", "This is five", "This is six"
)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
-6L), spec = structure(list(cols = list(Category = structure(list(), class = c("collector_character",
"collector")), description = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
如果您希望數據位於原始 data.frame/tibble 內的嵌套列表中,這將是一個選項:
library(dplyr)
library(tidyr)
library(data.trable)
# reading in your dummy data
df <- data.table::fread("Category description
Analysis 'This is one'
Hybrid 'This is two'
Other 'This is three'
Analysis 'This is four'
Other 'This is five'
Hybrid 'This is six'")
res <- df %>%
# helper line to prepare de read in data
tidyr::unite("description", description:V4, sep = " ") %>%
# build a grouping you need
dplyr::group_by(Category) %>%
# nest the wanted column to a list
tidyr::nest(description)
res
# A tibble: 3 x 2
# Groups: Category [3]
Category data
<chr> <list>
1 Analysis <tibble [2 x 1]>
2 Hybrid <tibble [2 x 1]>
3 Other <tibble [2 x 1]>
res[2][[1]]
[[1]]
# A tibble: 2 x 1
description
<chr>
1 'This is one'
2 'This is four'
[[2]]
# A tibble: 2 x 1
description
<chr>
1 'This is two'
2 'This is six'
[[3]]
# A tibble: 2 x 1
description
<chr>
1 'This is three'
2 'This is five'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.