R&sql merge records and go to heavy

Source: Internet
Author: User

======================================================================================

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.