Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr: How to handle multiple value

Tags:

r

dplyr

summarize

I have a dataframe, which return 2 groups for same min. How can it be handled to reach my expected output?

df<- read.table(header=TRUE,
                 text="
                 Company  xxx  yyyy  zzzz  cnt
                 abc       1     1    1     20
                 aaa       1     1    2     3
                 bbb       1     1    1     3
                 ddd       2     0    2     100
                 ")

i tried below code

final= df %>%
       group_by(xxx,yyyy) %>%
         summarise(Topcomp=Company[c(which(min(cnt)==cnt))])

Im getting:

Error: expecting a single value

I want to have output like below.

    xxx  yyyy Topcomp
  <int> <int>  <fctr>
1     1     1    aaa,bbb
2     2     0     ddd
like image 241
chilambu selvan Avatar asked Sep 14 '16 20:09

chilambu selvan


2 Answers

You should do this:

final= df %>%
   group_by(xxx,yyyy) %>%
     summarise(Topcomp=toString(Company[c(which(min(cnt)==cnt))]))
##Source: local data frame [2 x 3]
##Groups: xxx [?]
##
##    xxx  yyyy  Topcomp
##  <int> <int>    <chr>
##1     1     1 aaa, bbb
##2     2     0      ddd

You were getting the error because which returned two values so that your subset of Company has two values when summarise requires a single value. The toString is the similar to paste with collapse="," in that it collapses the two values into a string separated by the comma.

Also, as alistaire pointed out in his comment for the other answer, you don't need the c and the which, so this can be simplified to:

final= df %>%
  group_by(xxx,yyyy) %>%
    summarise(Topcomp=toString(Company[min(cnt)==cnt]))
like image 151
aichao Avatar answered Nov 12 '22 07:11

aichao


You could use paste(..., collapse = ",")

df %>%
  group_by(xxx,yyyy) %>%
  summarise(Topcomp = paste(Company[min(cnt) == cnt], collapse = ","))
like image 5
Richard Telford Avatar answered Nov 12 '22 08:11

Richard Telford