gpt4 book ai didi

R - 从列表中粘贴复杂的程式化值

转载 作者:行者123 更新时间:2023-12-04 11:14:44 25 4
gpt4 key购买 nike

我正在处理多个列表,每个列表都包含大量数据框。每个数据框包含3个变量(cluster, grp, value),如(example of 1 list)

$`0`
Source: local data frame [1 x 3]

cluster grp value
(int) (int) (chr)
1 1 0 c Personal Care-277

$`1`
Source: local data frame [1 x 3]

cluster grp value
(int) (int) (chr)
1 1 1 b Unpaid-1

$`2`
Source: local data frame [1 x 3]

cluster grp value
(int) (int) (chr)
1 1 2 c Personal Care-1

我想要的是将这些信息汇总在一个向量中,以便于分析它们 [output wanted] :

cluster 1 : (c Personal Care-277) - (b Unpaid-1) - (c Personal Care-1) 

我尝试做的是以下内容:

library(plyr)
library(dplyr)

1) 我首先通过cluster 将所有数据框合并在一起。我选择使用 join_all ,除了奇怪的 colname 输出外,它似乎很适合这项工作。

dt1 = dt %>% lapply(fgr) %>% 
join_all(by = 'cluster') %>%
`colnames<-`(c("cluster", paste('t', 1:3, sep = '')))

2) 然后我使用 paste 以程式化的方式将值放在一起

dt1 %>% 
mutate(print = paste('cluster: ', cluster, ' (' , t1, ')', '(', t2 , ')', '(', t3 , ')', sep="") ) %>%
select(print)

# print
# 1 cluster: 1 (c Personal Care-277)(b Unpaid-1)(c Personal Care-1)

问题是我有许多包含许多数据帧的不同列表,而一些数据帧的长度 不相等。这里示例中的列表有 3 个元素 t1 t2 t3(加上 cluster)。但是有些列表的数据框有 4 个或更多元素。

问题

我首先想知道是否有办法自动执行此粘贴,以避免编写t1t2手动,其次,如果您对例程有任何比我在这里展示的更好的想法。

谢谢

数据(列表)

dt = list(structure(list(cluster = structure(1L, .Label = "1", class = "factor"), 
grp = structure(1L, .Label = "0", class = "factor"), value = structure(1L, .Label = "c Personal Care-277", class = "factor")), .Names = c("cluster",
"grp", "value"), row.names = c(NA, -1L), class = "data.frame"),
structure(list(cluster = structure(1L, .Label = "1", class = "factor"),
grp = structure(1L, .Label = "1", class = "factor"),
value = structure(1L, .Label = "b Unpaid-1", class = "factor")), .Names = c("cluster",
"grp", "value"), row.names = c(NA, -1L), class = "data.frame"),
structure(list(cluster = structure(1L, .Label = "1", class = "factor"),
grp = structure(1L, .Label = "2", class = "factor"),
value = structure(1L, .Label = "c Personal Care-1", class = "factor")), .Names = c("cluster",
"grp", "value"), row.names = c(NA, -1L), class = "data.frame"))

最佳答案

你可以试试,

library(dplyr)
bind_rows(dt) %>%
group_by(cluster) %>%
summarise(new = paste0('cluster: ', unique(cluster), ' (', paste(value, collapse = ','), ')')) %>%
select(new)

# A tibble: 1 × 1
# new
# <chr>
#1 cluster: 1 (c Personal Care-277,b Unpaid-1,c Personal Care-1)

关于R - 从列表中粘贴复杂的程式化值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39290912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com