Home > Enterprise >  Group values from a column based on another column's values in R
Group values from a column based on another column's values in R

Time:04-06

I have a dataframe and I would like all values in the second column to be stored together when they have the same value in the first column. One of the difficulties is to put these values in quotation marks separated by semicolons only when there are several of them.

A   12
A   56
A   23
B   16
C   04
C   73

The result would be this :

A   "12;56;23"
B   16
C   "04;73"

I saw that the function fill() of tydir allows to do more or less the opposite of what I want, but I don't know any function able to do that. If you can give me some clues, Thanks !

CodePudding user response:

We can use paste with collapse after grouping

aggregate(col2 ~ col1, df1, \(x) 
    if(length(x) > 1) dQuote(paste(x, collapse =";"), FALSE) else x)

-output

 col1       col2
1    A "12;56;23"
2    B         16
3    C     "4;73"

data

df1 <- structure(list(col1 = c("A", "A", "A", "B", "C", "C"), col2 = c(12L, 
56L, 23L, 16L, 4L, 73L)), class = "data.frame", row.names = c(NA, 
-6L))
  • Related