0

我有一个这样的数据框:

   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")
4

1 回答 1

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
于 2013-06-08T02:09:16.730 回答