gpt4 book ai didi

R汇总字符数

转载 作者:行者123 更新时间:2023-12-04 01:02:36 24 4
gpt4 key购买 nike

    library(data.table)
DATA=data.table(STUDENT= c(1,2,3,4),
DOG_1= c("a","e","a","c"),
DOG_2= c("a","e","d","b"),
DOG_3= c("a","d","b","c"),
CAT_1= c("c","a","d","c"),
CAT_2= c("c","d","a","b"),
MOUSE_1= c("d","b","e","b"),
MOUSE_2= c("c","a","b","e"),
MOUSE_3= c("a","b","b","e"),
MOUSE_4= c("b","c","a","d"))

这就是我的数据在上面的样子。我希望得到一个看起来像这样的新数据:

enter image description here

其中“a”等于 1; 'b' 等于 2; 'c' 等于 3; 'd' 等于 4; 'e' 等于 5,例如 STUDENT 1 DOG 等于 3 的值是通过将字母转换为值并求和得出的。

最佳答案

如果我们要使用data.table解决方案,melt 'DATA',通过指定patterns从列名变成' long' 格式,然后使用命名的 vector ('keyval'),按 'STUDENT 分组,循环遍历 .SDcols 中指定的列,匹配并用整数值和 sum

library(data.table)
nm1 <- unique(sub("_\\d+$", "", names(DATA)[-1]))
dt1 <- melt(DATA, id.var = 'STUDENT',
measure = patterns(nm1), value.name = nm1)
keyval <- setNames(1:5, letters[1:5])

dt1[, lapply(.SD, function(x) sum(keyval[x],
na.rm = TRUE)), by = STUDENT, .SDcols = nm1]

-输出

#   STUDENT DOG CAT MOUSE
#1: 1 3 6 10
#2: 2 14 5 8
#3: 3 7 5 10
#4: 4 8 5 16

tidyverse 中的类似选项是

library(dplyr)
library(tidyr)
DATA %>%
pivot_longer(cols = -STUDENT, names_to = c('.value', 'grp'),
names_sep='_') %>%
group_by(STUDENT) %>%
summarise(across(all_of(nm1), ~ sum(keyval[.], na.rm = TRUE)))
# A tibble: 4 x 4
# STUDENT DOG CAT MOUSE
# <dbl> <int> <int> <int>
#1 1 3 6 10
#2 2 14 5 8
#3 3 7 5 10
#4 4 8 5 16

关于R汇总字符数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67745336/

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