gpt4 book ai didi

r - 成对组合以及 data.table 中的计数

转载 作者:行者123 更新时间:2023-12-04 11:56:37 24 4
gpt4 key购买 nike

我有一个数据框 d如下。

d <- structure(list(sno = 1:7, list = c("SD1, SD44, SD384, SD32", 
"SD23, SD1, SD567", "SD42, SD345, SD183", "SD345, SD340, SD387",
"SD455, SD86, SD39", "SD12, SD315, SD387", "SD32, SD1, SD40")), .Names = c("sno",
"list"), row.names = c(NA, -7L), class = "data.frame")

d
sno list
1 1 SD1, SD44, SD384, SD32
2 2 SD23, SD1, SD567
3 3 SD42, SD345, SD183
4 4 SD345, SD340, SD387
5 5 SD455, SD86, SD39
6 6 SD12, SD315, SD387
7 7 SD32, SD1, SD40

我想在 d$list 中获得由“,”分隔的所有字符串的成对组合.

我可以使用 lapply 获得它如下。
d2 <- strsplit(d$list, split = ", ")
d2 <- lapply(d2, function(x) as.data.frame(t(combn(x, m=2))))
library(data.table)
d2 <- rbindlist(d2)

我不想在 d$list 中获得每个组的计数连同合并列表 d2作为一个新列。如何使用 data.table 执行此操作?
library(stringi)
stri_count_fixed(d$list,", ")

所需的输出如下
out <- structure(list(V1 = structure(c(1L, 1L, 1L, 3L, 3L, 2L, 4L, 4L, 
1L, 6L, 6L, 5L, 5L, 5L, 7L, 8L, 8L, 9L, 10L, 10L, 11L, 12L, 12L,
1L), .Label = c("SD1", "SD384", "SD44", "SD23", "SD345", "SD42",
"SD340", "SD455", "SD86", "SD12", "SD315", "SD32"), class = "factor"),
V2 = structure(c(3L, 2L, 1L, 2L, 1L, 1L, 4L, 5L, 5L, 7L,
6L, 6L, 8L, 9L, 9L, 11L, 10L, 10L, 12L, 9L, 9L, 4L, 13L,
13L), .Label = c("SD32", "SD384", "SD44", "SD1", "SD567",
"SD183", "SD345", "SD340", "SD387", "SD39", "SD86", "SD315",
"SD40"), class = "factor"), count = c(4, 4, 4, 4, 4, 4, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)), .Names = c("V1",
"V2", "count"), row.names = c(NA, -24L), class = "data.frame")


out
V1 V2 count
1 SD1 SD44 4
2 SD1 SD384 4
3 SD1 SD32 4
4 SD44 SD384 4
5 SD44 SD32 4
6 SD384 SD32 4
7 SD23 SD1 3
8 SD23 SD567 3
9 SD1 SD567 3
10 SD42 SD345 3
11 SD42 SD183 3
12 SD345 SD183 3
13 SD345 SD340 3
14 SD345 SD387 3
15 SD340 SD387 3
16 SD455 SD86 3
17 SD455 SD39 3
18 SD86 SD39 3
19 SD12 SD315 3
20 SD12 SD387 3
21 SD315 SD387 3
22 SD32 SD1 3
23 SD32 SD40 3
24 SD1 SD40 3

最佳答案

使用 gsub我们可以删除除分隔符( , )之外的所有字符,用 nchar 计算字符数, 添加 1获取单词数,并使用 transform 创建一个新列 'Count' .使用 cSplit来自 splitstackshape ,我们可以通过 , 拆分“列表”列, 通过将方向指定为 long ,我们重新格式化数据集。加载中 splitstackshape还将加载 data.table ,所以我们可以使用 data.table 的聚合方法。按 'sno' 和 'Count' ( .(sno, Count) ) 分组,我们得到 combn 'list',基于来自 combn 的交替值创建两列('V1'、'V2')输出,并将“sno”列分配给 NULL(如果不需要)

library(splitstackshape)
d1 <- transform(d, Count=nchar(gsub('[^,]', '', list))+1L)
cSplit(d1, 'list', ', ', 'long')[, {
tmp <- combn(as.character(list), 2)
list(V1=tmp[c(TRUE, FALSE)], V2= tmp[c(FALSE, TRUE)])
}, .(sno, Count)][,
sno:= NULL]
# Count V1 V2
#1: 4 SD1 SD44
#2: 4 SD1 SD384
#3: 4 SD1 SD32
#4: 4 SD44 SD384
#5: 4 SD44 SD32
#6: 4 SD384 SD32
#7: 3 SD23 SD1
#8: 3 SD23 SD567
#9: 3 SD1 SD567
#10: 3 SD42 SD345
#11: 3 SD42 SD183
#12: 3 SD345 SD183
#13: 3 SD345 SD340
#14: 3 SD345 SD387
#15: 3 SD340 SD387
#16: 3 SD455 SD86
#17: 3 SD455 SD39
#18: 3 SD86 SD39
#19: 3 SD12 SD315
#20: 3 SD12 SD387
#21: 3 SD315 SD387
#22: 3 SD32 SD1
#23: 3 SD32 SD40
#24: 3 SD1 SD40

或者修改您的代码,我们使用 Map/cbind 在“d2”中创建“Count”列并如帖子中所述,执行 rbindlist折叠 list到单个“data.table”对象。
library(stringi)
library(data.table)
Count <- stri_count_fixed(d$list,", ")+1
d2 <- strsplit(d$list, split = ", ")
d2 <- lapply(d2, function(x) as.data.frame(t(combn(x, m=2))))
rbindlist(Map(cbind, d2, Count=Count))
# V1 V2 Count
# 1: SD1 SD44 4
# 2: SD1 SD384 4
# 3: SD1 SD32 4
# 4: SD44 SD384 4
# 5: SD44 SD32 4
# 6: SD384 SD32 4
# 7: SD23 SD1 3
# 8: SD23 SD567 3
# 9: SD1 SD567 3
#10: SD42 SD345 3
#11: SD42 SD183 3
#12: SD345 SD183 3
#13: SD345 SD340 3
#14: SD345 SD387 3
#15: SD340 SD387 3
#16: SD455 SD86 3
#17: SD455 SD39 3
#18: SD86 SD39 3
#19: SD12 SD315 3
#20: SD12 SD387 3
#21: SD315 SD387 3
#22: SD32 SD1 3
#23: SD32 SD40 3
#24: SD1 SD40 3

关于r - 成对组合以及 data.table 中的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30702191/

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