Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use dplyr to concatenate a column [duplicate]

Tags:

r

dplyr

I have a data_frame where I would like vector to be the concatenation of elements in A. So

df <- data_frame(id = c(1, 1, 2, 2), A = c("a", "b", "b", "c"))
df
Source: local data frame [4 x 2]

  id A
1  1 a
2  1 b
3  2 b
4  2 c

Should become

newdf
Source: local data frame [4 x 2]

  id vector
1  1 "a b"
2  2 "b c"

My first inclination is to use paste() inside summarise but this doesn't work.

df %>% group_by(id) %>% summarise(paste(A))
Error: expecting a single value

Hadley and Romain talk about a similar issue in the GitHub issues, but I can't quite see how that applies directly. It seems like there should be a very simple solution, especially because paste() usually does return a single value.

like image 628
gregmacfarlane Avatar asked Feb 26 '15 21:02

gregmacfarlane


2 Answers

You need to collapse the values in paste

df %>% group_by(id) %>% summarise(vector=paste(A, collapse=" "))
like image 62
MrFlick Avatar answered Oct 13 '22 18:10

MrFlick


My data frame was as:
col1 col2

1           one 
1           one more
2           two
2           two
3           three

I needed to summarise it as follows:

col1 col3

1           one, one more
2           two
3           three

This following code did the trick:

    df <- data.frame(col1 = c(1,1,2,2,3), col2 = c("one", "one more", "two", "two", "five"))

    df %>%
            group_by(col1) %>%
            summarise( col3 = toString(unique(col2)))
like image 35
Nasir Avatar answered Oct 13 '22 19:10

Nasir