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
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]))
You could use paste(..., collapse = ",")
df %>%
group_by(xxx,yyyy) %>%
summarise(Topcomp = paste(Company[min(cnt) == cnt], collapse = ","))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With