mbs1
mbs1

Reputation: 317

Making new column with multiple elements after group_by

I'm trying to make a new column as described below. the d's actually correspond to dates and V2 are events on the given dates. I need to collect the events for the given date. V3 is a single column whose row entries are a concatenation. Thanks in advance. My attempt does not work.

 df =   V1    V2 
        d1    U
        d2    M
        d1    T
        d1    Q
        d2    P

desired resulting df

  df.1 = V1      V3
         d1      U,T,Q
         d2      M,P


 df.1 <- df %>% group_by(., V1) %>%
         mutate(., V3 = c(distinct(., V2))) %>%
         as.data.frame

The above code results in the following error; ignore the 15 and 1s--they're specific to my actual code

Error: incompatible size (15), expecting 1 (the group size) or 1

Upvotes: 0

Views: 43

Answers (3)

Vincent Bonhomme
Vincent Bonhomme

Reputation: 7443

still with dplyr, you can try:

df %>% group_by(V1) %>% summarize(V3 = paste(unique(V2), collapse=", "))

Upvotes: 0

akuiper
akuiper

Reputation: 214957

It will not allow a vector as an element in data frame. So instead of using c(), you can use paste to concatenate elements as a single string.

df.1 <- df %>% group_by(V1) %>% mutate(V3 = paste(unique(V2), collapse = ",")) %>% select(V1, V3) %>% unique() %>% as.data.frame()

Upvotes: 0

Wyldsoul
Wyldsoul

Reputation: 1553

You can use aggregate like this:

df.1 <- aggregate(V2~V1,paste,collapse=",",data=df)

#  V1    V2
# 1 d1 U,T,Q
# 2 d2   M,P

Upvotes: 1

Related Questions