ronnydw
ronnydw

Reputation: 953

How to keep factor strings after dplyr summarise?

For the following example:

library(dplyr)

df <- data.frame(v1 = c("a", "a", "b", "b"), v2 = c(3,3,4,4), v3 = c(11,21,31,41))
df

  v1 v2 v3
1  a  3 11
2  a  3 21
3  b  4 31
4  b  4 41

I want to summarise v3 on v2 and keep v1, with:

df %>% group_by(v2) %>% summarise(v1 = first(v1), s3 = sum(v3))

and get:

  v2 v1 s3
1  3  1 32
2  4  2 72

but I want:

  v1 v2 s3
1  a  3 32
2  b  4 72

Any idea how I cn keep the factor strings iso id's and the same order of the columns?

Upvotes: 3

Views: 1619

Answers (2)

SDahm
SDahm

Reputation: 436

You were so close. Here the dplyr solution to your question.

df %>% group_by(v1,v2) %>% summarise(s3 = sum(v3))

Upvotes: 0

rafa.pereira
rafa.pereira

Reputation: 13807

Here is a simple and fast data.table solution.

library(data.table)
setDT(df)

# summarize
  output <- df[, .( v1=v1[1] , s3=sum(v3)), by=v2]

# reorder columns
  setcolorder(output, c("v1", "v2", "s3"))

#    v1 v2 s3
# 1:  a  3 32
# 2:  b  4 72

Upvotes: 1

Related Questions