加上乐趣=';c';或';列表';在R

加上乐趣=';c';或';列表';在R,r,list,concatenation,aggregate-functions,aggregate,R,List,Concatenation,Aggregate Functions,Aggregate,一直在四处寻找,但到目前为止运气不佳 这是数据框 > test = data.frame(x = c(1,1,2,2,3,3), y = c('a','b','c','d','e','f')) > test x y 1 1 a 2 1 b 3 2 c 4 2 d 5 3 e 6 3 f 正在寻找一种聚合的方法,以便将具有相同x值的y组成一个列表或向量 差不多 x y 1 1 a,b 2 2 c,d 3 3 e,f 尝试了“c”,但结果不是预期的结果 > aggreg

一直在四处寻找,但到目前为止运气不佳

这是数据框

> test = data.frame(x = c(1,1,2,2,3,3), y = c('a','b','c','d','e','f'))
> test
  x y
1 1 a
2 1 b
3 2 c
4 2 d
5 3 e
6 3 f
正在寻找一种聚合的方法,以便将具有相同x值的y组成一个列表或向量

差不多

  x y
1 1 a,b
2 2 c,d
3 3 e,f
尝试了“c”,但结果不是预期的结果

> aggregate(y~x, data = test, FUN = 'c')
  x y.1 y.2
1 1   1   2
2 2   3   4
3 3   5   6
“列表”似乎有效,但它将字符转换为因子

> ss = aggregate(y~x, data = test, FUN = 'list')
> class(ss$y[1][[1]])
[1] "factor"
> ss$y[1]
$`1`
[1] a b
Levels: a b c d e f

非常感谢您的任何评论。

这里有一种使用base R的方法

res <-lapply(split(test, test$x), function(xx) data.frame(x=unique(xx$x),
   y=paste(xx$y, collapse=", ")))
do.call(rbind, res)
  x    y
1 1 a, b
2 2 c, d
3 3 e, f

res作为
数据中的默认设置,“测试”数据中的列“y”是
因子(由@bondedust提及)。frame
调用是
stringsAsFactors=TRUE
。因此,它没有将
字符
转换为
因子
。如果我们在创建
数据.frame
时使用
stringsafactors=FALSE
,则
将是
字符
,并保持不变

test = data.frame(x = c(1,1,2,2,3,3), y = c('a','b','c','d','e','f'), 
           stringsAsFactors=FALSE)
res <- aggregate(y~x, data = test, FUN = 'list')
str(res)
#'data.frame':  3 obs. of  2 variables:
#$ x: num  1 2 3
# $ y:List of 3
# ..$ 1: chr  "a" "b"
# ..$ 2: chr  "c" "d"
# ..$ 3: chr  "e" "f"

或者我们可以使用
data.table
作为替代方法。我们将'data.frame'转换为'data.table'(
setDT(test)
),按'x'分组,然后列出'y'元素

library(data.table)
setDT(test)[, list(y=list(y)), by = x]

您可以从
tidyr
使用
nest

library(tidyr)

nest(test, y)

Source: local data frame [3 x 2]
Groups: <by row>

      x           y
  (dbl)       (chr)
1     1 <S3:factor>
2     2 <S3:factor>
3     3 <S3:factor>

你知道吗,
y
-列最初是作为因子向量的?谢谢你提到这一点。这对我来说是新的。如果你想把结果作为列表保存,那么只需在参数中添加simply=FALSE:
ss=aggregate(y~x,data=test,FUN=list,simplify=FALSE)
library(tidyr)

nest(test, y)

Source: local data frame [3 x 2]
Groups: <by row>

      x           y
  (dbl)       (chr)
1     1 <S3:factor>
2     2 <S3:factor>
3     3 <S3:factor>
[[1]]
[1] a b
Levels: a b c d e f

[[2]]
[1] c d
Levels: a b c d e f

[[3]]
[1] e f
Levels: a b c d e f