数据操作:减少行数(使用 R)

Data manipulation: reduce rows number (with R)

提问人:Boh 提问时间:9/22/2023 更新时间:10/4/2023 访问量:28

问:

我的数据集是这样的:

a1 <- c("a","a","a", "b", "c", "c", "d", "d", "d", "d")
b1 <- c(7, 7, 7,5, 4, 4, 3, 3, 3, 3)
c1 <- c("A","B", "C", "D", "E", "F", "B", "C", "EE", "F")
m1 <- data.frame(a1, b1, c1)

我的预期结果是这样的:

a <- c("a","b", "c", "d")
b <- c(7, 5, 4, 3)
c <- c("A; B; C","D", "E; F", "B; C; EE; F")
m <- data.frame(a, b, c)

我的实际代码是这样的,但它不起作用:

library(dplyr)
m2 <-m |> 
  summarise(c = gsub(", ", "", toString(c1)), .by=c(a1,b1)) 

我该如何解决? 谢谢

r 数据操作

评论


答:

2赞 ThomasIsCoding 9/22/2023 #1

我想你可以试试

> aggregate(c1 ~ ., m1, toString)
  a1 b1          c1
1  d  3 B, C, EE, F
2  c  4        E, F
3  b  5           D
4  a  7     A, B, C
0赞 Umar 10/4/2023 #2
m2 <- m1 %>%
  group_by(a1, b1) %>%
  summarise(c = paste(c1, collapse = "; ")) %>%
  ungroup() %>%
  distinct()

colnames(m2) <- c("a", "b", "c")

print(m2)

这将给出预期的结果

a b           c
1 a 7     A; B; C
2 b 5           D
3 c 4        E; F
4 d 3 B; C; EE; F