R:使用c汇总字符串

Roy*_*012 1 aggregate r

我有一个包含两列的数据框:一列是字符串,另一列是整数.

> rnames = sapply(1:20, FUN=function(x) paste("item", x, sep="."))
> x <- sample(c(1:5), 20, replace = TRUE)
> df <- data.frame(x, rnames)
> df
   x  rnames
1  5  item.1
2  3  item.2
3  5  item.3
4  3  item.4
5  1  item.5
6  3  item.6
7  4  item.7
8  5  item.8
9  4  item.9
10 5 item.10
11 5 item.11
12 2 item.12
13 2 item.13
14 1 item.14
15 3 item.15
16 4 item.16
17 5 item.17
18 4 item.18
19 1 item.19
20 1 item.20
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用'c'或'list'函数将字符串聚合到列表或字符串(字符)的向量中,但得到奇怪的结果:

> aggregate(rnames ~ x, df, c)
  x             rnames
1 1      16, 6, 11, 13
2 2               4, 5
3 3      12, 15, 17, 7
4 4      18, 20, 8, 10
5 5 1, 14, 19, 2, 3, 9
Run Code Online (Sandbox Code Playgroud)

当我使用'paste'而不是'c'时,我可以看到聚合工作正常 - 但结果不是我正在寻找的.

> aggregate(rnames ~ x, df, paste)
  x                                            rnames
1 1                 item.5, item.14, item.19, item.20
2 2                                  item.12, item.13
3 3                   item.2, item.4, item.6, item.15
4 4                  item.7, item.9, item.16, item.18
5 5 item.1, item.3, item.8, item.10, item.11, item.17
Run Code Online (Sandbox Code Playgroud)

我正在寻找的是每个聚合组将被呈现为向量或点亮(因此使用c),而不是我使用'paste'获得的单个字符串.下面的内容(实际上不起作用):

> aggregate(rnames ~ x, df, c)
  x                                            rnames
1 1                 item.5, item.14, item.19, item.20
2 2                                  item.12, item.13
3 3                   item.2, item.4, item.6, item.15
4 4                  item.7, item.9, item.16, item.18
5 5 item.1, item.3, item.8, item.10, item.11, item.17
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

pla*_*pus 5

你陷入了通常的陷阱data.frame:你的角色列不是一个字符列,它是一个因子列!因此数字而不是结果中的字符:

> rnames = sapply(1:20, FUN=function(x) paste("item", x, sep="."))
> x <- sample(c(1:5), 20, replace = TRUE)
> df <- data.frame(x, rnames)
> str(df)
'data.frame':   20 obs. of  2 variables:
 $ x     : int  2 5 5 5 5 4 3 3 2 4 ...
 $ rnames: Factor w/ 20 levels "item.1","item.10",..: 1 12 14 15 16 17 18 19 20 2 ...
Run Code Online (Sandbox Code Playgroud)

要阻止转换为因子,请stringAsFactors=FALSE在调用中使用参数data.frame:

> df <- data.frame(x, rnames,stringsAsFactors=FALSE)
> str(df)
'data.frame':   20 obs. of  2 variables:
 $ x     : int  5 5 3 5 5 3 2 5 1 5 ...
 $ rnames: chr  "item.1" "item.2" "item.3" "item.4" ...
> aggregate(rnames ~ x, df, c)
  x                                                                              rnames
1 1                                                            item.9, item.13, item.17
2 2                                                                              item.7
3 3                                                             item.3, item.6, item.19
4 4                                                           item.12, item.15, item.16
5 5 item.1, item.2, item.4, item.5, item.8, item.10, item.11, item.14, item.18, item.20
Run Code Online (Sandbox Code Playgroud)

另一种避免转换为因子的解决方案是功能I:

> df <- data.frame(x, I(rnames))
> str(df)
'data.frame':   20 obs. of  2 variables:
 $ x     : int  3 5 4 5 4 5 3 3 1 1 ...
 $ rnames:Class 'AsIs'  chr [1:20] "item.1" "item.2" "item.3" "item.4" ...
Run Code Online (Sandbox Code Playgroud)

摘录自?I:

在函数data.frame中.通过在对Data.frame的调用中将其封装在I()中来保护对象会禁止将字符向量转换为因子并删除名称,并确保将矩阵作为单个列插入.我还可以用来保护要添加到数据帧的对象,或者通过as.data.frame转换为数据帧.

它通过将类"AsIs"添加到对象的类来实现这一点."AsIs"类有一些自己的方法,包括[,as.data.frame,print和format.