简体   繁体   中英

R list all distinct values for each value of another variable

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.

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

dplyr它看起来像:

numbers %>% group_by(sort.nums.) %>% summarise( values= paste(nums2,collapse=",") ) 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM