======================================================================================
Data
There are <-data.frame (id=paste0 ("ID", C (101, 102, 102, 103, 103, 104, 105, 105)),
Info1=c ("One", "Twoa", "Twob", "Threea", "Threeb", "Four", "five", "five"),
Stringsasfactors=false)
Want <-Data.frame (id=paste0 ("ID", C (101:105)),
Info1=c ("One", "TWOA; Twob "," THREEA; Threeb "," Four "," five "),
Stringsasfactors=false)
======================================================================================
Mysql
SELECT user_id , COUNT as Freq, Group_concat (DISTINCT as IPsfrom log_table GROUPby user_idORDERby ASC;
======================================================================================
R&sql
Library (SQLDF)
Sqldf ("
SELECT ID,
Group_concat (distinct info1) as Info1
From has
GROUP by ID ")
Note: The change interval is not implemented in this case, and the default is a comma.
======================================================================================
R
--------------------------------------------------------------------------------------------------------------- -----------------------
Require (DPLYR)
METHOD1: Has %>% group_by (ID)%>% Summarise_each (funs (toString (sort (info1 ))))
METHOD2: Has %>% group_by (ID)%>% summarise (name = toString (sort (Unique (INFO1) )))
--------------------------------------------------------------------------------------------------------------- -----------------------
Require (data.table)
METHOD1: Setdt (have) [,. ( Info1 = toString (sort (info1))), by = ID]
METHOD2: Setdt (have) [,. ( Info1 = Paste (Unique (info1), collapse = ",")), by = ID]
Note: The paste function cannot be used as an argument to other functions. For example
Aggregate (have[,2], by=list (have$id), paste (unique (INFO1)), collapse= ";") x
Aggregate (have[,2], by=list (have$id), paste, collapse= ";") √
SETDT (have) [, lapply (. SD, paste (unique (info1)), collapse = ";"), by = ID] x
SETDT (have) [, lapply (. SD, paste, collapse = ";"), by = ID] √
--------------------------------------------------------------------------------------------------------------- -----------------------
Aggregate (Data=have,info1~id,fun = function (t) sort (unique (t)))
R&sql merge records and go to heavy