Reputation: 181
I would like to list all distinct values associated with values of another variable. For instance:
nums <- rep(1:3,5)
nums2 <- 142:156
numbers <- data.frame(sort(nums),nums2)
nums nums2
1 1 142
2 1 143
3 1 144
4 1 145
5 1 146
6 2 147
7 2 148
8 2 149
9 2 150
10 2 151
11 3 152
12 3 153
13 3 154
14 3 155
15 3 156
Now I want to figure out how to list all unique values of nums2
for each unique value of nums
such that i can get a list like this:
nums values
1 1 142,143,144,145,146
2 2 147,148,149,150,151
3 3 152,153,154,155,156
Any help is appreciated.
Upvotes: 0
Views: 798
Reputation: 887951
Here is an option using data.table
library(data.table)
unique(setDT(numbers))[, .(values = toString(nums2)), by = nums]
# nums values
#1: 1 142, 143, 144, 145, 146
#2: 2 147, 148, 149, 150, 151
#3: 3 152, 153, 154, 155, 156
Upvotes: 0
Reputation: 19544
You can use aggregate
together with paste
for this:
aggregate(cbind(values=nums2) ~ nums, numbers, paste, collapse=",")
nums values
1 1 142,145,148,151,154
2 2 143,146,149,152,155
3 3 144,147,150,153,156
Upvotes: 4
Reputation: 2939
In dplyr
this would look like:
numbers %>% group_by(sort.nums.) %>% summarise( values= paste(nums2,collapse=",") )
Upvotes: 0
Reputation: 32558
setNames(data.frame(do.call(rbind, lapply(split(numbers, numbers$sort.nums.),
function(a) c(a$sort.nums[1], paste(a$nums2, collapse = ", "))))),
nm = c("nums","values"))
Upvotes: 1