R:ddply并返回字符串

时间:2013-06-08 01:06:27

标签: r plyr

我有一个这样的数据框:

   id col1
1   1    1
2   2    2
3   3    3
4   4    4
5   5    1
6   1    2
7   2    3
8   3    4

我想按id分组,然后创建一个字符串,其中包含col1中以空格和降序值分隔的值。

我首先按id和col1对数据框进行排序,但是无法将ddply的输出作为没有引号的字符串获取。

df111 <- df111[order(df111$id, -df111$col1),]

df222 <- ddply(df111, .(id), function(col1) as.character(paste0(col1,sep = ' ')))

  id             V1                                                                               V2
1  1 c(1, 1, 1, 1)    c(0.793507214868441, 0.539258575299755, 0.165128685068339, 0.153290810529143) 
2  2 c(2, 2, 2, 2)    c(0.872032727580518, 0.827515688957646, 0.236087603960186, 0.165240615839139) 
3  3 c(3, 3, 3, 3)   c(0.759382889838889, 0.484359077410772, 0.182580581633374, 0.0723447729833424) 
4  4 c(4, 4, 4, 4)  c(0.874859027564526, 0.642130059422925, 0.0569298807531595, 0.0227038362063468) 
5  5 c(5, 5, 5, 5)    c(0.392553070792928, 0.386064056074247, 0.299609177513048, 0.222290486795828) 

我喜欢这样的事情:

    id     V1
1   1   .793507214868441 0.539258575299755 0.165128685068339 0.153290810529143

有什么建议吗?

编辑:

> dput(df111)
structure(list(id = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), col1 = c(0.793507214868441, 
0.539258575299755, 0.165128685068339, 0.153290810529143, 0.872032727580518, 
0.827515688957646, 0.236087603960186, 0.165240615839139, 0.759382889838889, 
0.484359077410772, 0.182580581633374, 0.0723447729833424, 0.874859027564526, 
0.642130059422925, 0.0569298807531595, 0.0227038362063468, 0.392553070792928, 
0.386064056074247, 0.299609177513048, 0.222290486795828)), .Names = c("id", 
"col1"), row.names = c(1L, 11L, 16L, 6L, 7L, 12L, 17L, 2L, 18L, 
13L, 8L, 3L, 14L, 9L, 19L, 4L, 20L, 10L, 5L, 15L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

我想也许你只需要使用summarise而不是自定义匿名函数......?

dat <- read.table(text = "id col1
 1   1    1
 2   2    2
 3   3    3
 4   4    4
 5   5    1
 6   1    2
 7   2    3
 8   3    4",header = TRUE,sep = "")
> ddply(dat,.(id),summarise,val = paste(sort(col1,decreasing = TRUE),collapse = " "))
  id val
1  1 2 1
2  2 3 2
3  3 4 3
4  4   4
5  5   1