tgodoy
2012-Aug-03 17:00 UTC
[R] How to concatenate a several rows according with a column ?
Hi, I'm a new user or R and I try to concatenate a several rows according with the value in a column. this is my data.frame and I want to concatenate my data.frame according with the column "b" and make a new data.frame with the information in the others columns.>table1a b c d 1 E001234 TSA IP234 like_domain 2 E001234 TSB IP234 like_domain 3 E001234 TSC IP234 like_domain 4 E001234 TSD IP234 like_domain 5 E001235 TSA IP235 two_domain 6 E001235 TSD IP235 two_domain 7 E001235 TSS IP235 two_domain 8 E001236 TSP IP236 like_domain 9 E001236 TST IP236 like_domain 10 E001237 TSV IP237 simple_domain I want my table in this way>table_newa b c d 1 E001234 TSA, TSB, TSC, TSD IP234 like_domain 2 E001235 TSA, TSD, TSS IP235 two_domain 3 E001236 TSP, TSP, TST IP236 like_domain 4 E001237 TSV IP237 simple_domain How can I do this in R? Thanks -- View this message in context: http://r.789695.n4.nabble.com/How-to-concatenate-a-several-rows-according-with-a-column-tp4639072.html Sent from the R help mailing list archive at Nabble.com.
Jean V Adams
2012-Aug-03 21:38 UTC
[R] How to concatenate a several rows according with a column ?
tgodoy <tingola_07@hotmail.com> wrote on 08/03/2012 12:00:24 PM:> > Hi, I'm a new user or R and I try to concatenate a several rowsaccording> with the value in a column. > > this is my data.frame and I want to concatenate my data.frame accordingwith> the column "b" and make a new data.frame with the information in theothers> columns. > > >table1 > a b c d > 1 E001234 TSA IP234 like_domain > 2 E001234 TSB IP234 like_domain > 3 E001234 TSC IP234 like_domain > 4 E001234 TSD IP234 like_domain > 5 E001235 TSA IP235 two_domain > 6 E001235 TSD IP235 two_domain > 7 E001235 TSS IP235 two_domain > 8 E001236 TSP IP236 like_domain > 9 E001236 TST IP236 like_domain > 10 E001237 TSV IP237 simple_domainThanks for providing example data! Very helpful. You can make things even easier on r-help readers by using the dput() function to create code for us to easily recreate your data using copy/paste. For example ... table1 <- structure(list(a = c("E001234", "E001234", "E001234", "E001234", "E001235", "E001235", "E001235", "E001236", "E001236", "E001237"), b = c("TSA", "TSB", "TSC", "TSD", "TSA", "TSD", "TSS", "TSP", "TST", "TSV"), c = c("IP234", "IP234", "IP234", "IP234", "IP235", "IP235", "IP235", "IP236", "IP236", "IP237"), d = c("like_domain", "like_domain", "like_domain", "like_domain", "two_domain", "two_domain", "two_domain", "like_domain", "like_domain", "simple_domain")), .Names = c("a", "b", "c", "d"), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"))> I want my table in this way > > >table_new > a b > c > d > 1 E001234 TSA, TSB, TSC, TSD IP234 like_domain > 2 E001235 TSA, TSD, TSS IP235 two_domain > 3 E001236 TSP, TSP, TST IP236 like_domain > 4 E001237 TSV IP237 > simple_domain > > How can I do this in R? > > ThanksYou can do this with the aggregate() and paste() functions in one fell swoop. aggregate(b ~ a + c + d, data=table1, paste, sep=",") Jean [[alternative HTML version deleted]]
arun
2012-Aug-03 23:47 UTC
[R] How to concatenate a several rows according with a column ?
Hi,
You could also use ddply.
Though, the output format differs a bit from aggregate().
dat1<-read.table(text="
?? a??????????????????? b???????? c???????????? d
????? E001234????? TSA??? IP234?? like_domain
????? E001234????? TSB??? IP234?? like_domain???
????? E001234????? TSC??? IP234?? like_domain
????? E001234????? TSD??? IP234?? like_domain
????? E001235????? TSA??? IP235?? two_domain
????? E001235????? TSD??? IP235?? two_domain
????? E001235????? TSS??? IP235?? two_domain
????? E001236????? TSP??? IP236?? like_domain
????? E001236????? TST??? IP236?? like_domain
??? E001237????? TSV??? IP237?? simple_domain
",sep="",header=TRUE,stringsAsFactors=FALSE)
?dat2<-ddply(dat1,.(a,c,d), paste,sep=",")
?dat2<-dat2[,c(1,5,2:3)]
colnames(dat2)<-colnames(dat1)
?dat2
??????? a???????????????????????????? b???? c???????????? d
1 E001234 c("TSA", "TSB", "TSC", "TSD")
IP234?? like_domain
2 E001235??????? c("TSA", "TSD", "TSS") IP235???
two_domain
3 E001236?????????????? c("TSP", "TST") IP236?? like_domain
4 E001237?????????????????????????? TSV IP237 simple_domain
A.K.
----- Original Message -----
From: tgodoy <tingola_07 at hotmail.com>
To: r-help at r-project.org
Cc:
Sent: Friday, August 3, 2012 1:00 PM
Subject: [R] How to concatenate a several rows according with a column ?
Hi, I'm a new user or R and I try to concatenate a several rows according
with the value in a column.
this is my data.frame and I want to concatenate my data.frame according with
the column "b" and make a new data.frame with the information in the
others
columns.
>table1
? ? ? ? a? ? ? ? ? ? ? ? ? ? b? ? ? ? c? ? ? ? ? ? d
1? ? ? E001234? ? ? TSA? ? IP234? like_domain
2? ? ? E001234? ? ? TSB? ? IP234? like_domain? ?
3? ? ? E001234? ? ? TSC? ? IP234? like_domain
4? ? ? E001234? ? ? TSD? ? IP234? like_domain
5? ? ? E001235? ? ? TSA? ? IP235? two_domain
6? ? ? E001235? ? ? TSD? ? IP235? two_domain
7? ? ? E001235? ? ? TSS? ? IP235? two_domain
8? ? ? E001236? ? ? TSP? ? IP236? like_domain
9? ? ? E001236? ? ? TST? ? IP236? like_domain
10? ? E001237? ? ? TSV? ? IP237? simple_domain
I want my table in this way
>table_new
? ? ? ? a? ? ? ? ? ? ? ? ? ? b? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? c? ? ? ? ?
?
d
1? ? ? E001234? ? ? TSA, TSB, TSC, TSD? ? IP234? like_domain
2? ? ? E001235? ? ? ? ? ? ? ? TSA, TSD, TSS? ? IP235? two_domain
3? ? ? E001236? ? ? ? ? ? ? ? TSP, TSP, TST? ? IP236? like_domain
4? ? ? E001237? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TSV? ? IP237?
simple_domain
How can I do this in R?
Thanks
--
View this message in context:
http://r.789695.n4.nabble.com/How-to-concatenate-a-several-rows-according-with-a-column-tp4639072.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
arun
2012-Aug-04 05:08 UTC
[R] How to concatenate a several rows according with a column ?
HI,
Try this:
#Reformatted the ddply results to match your desired output.
dat1<-read.table(text="
?? a??????????????????? b???????? c???????????? d
????? E001234????? TSA??? IP234?? like_domain
????? E001234????? TSB??? IP234?? like_domain???
????? E001234????? TSC??? IP234?? like_domain
????? E001234????? TSD??? IP234?? like_domain
????? E001235????? TSA??? IP235?? two_domain
????? E001235????? TSD??? IP235?? two_domain
????? E001235????? TSS??? IP235?? two_domain
????? E001236????? TSP??? IP236?? like_domain
????? E001236????? TST??? IP236?? like_domain
??? E001237????? TSV??? IP237?? simple_domain
",sep="",header=TRUE,stringsAsFactors=FALSE)
dat2<-ddply(dat1,.(a,c,d), paste,sep=",")
dat2<-dat2[,c(1,5,2:3)]
colnames(dat2)<-colnames(dat1)
dat3<-data.frame(sapply(dat2,function(x)
gsub("c|\\(|\\\"|\\)","",x)))
dat3
#??????? a????????????????? b???? c???????????? d
#1 E001234 TSA, TSB, TSC, TSD IP234?? like_domain
#2 E001235????? TSA, TSD, TSS IP235??? two_domain
#3 E001236?????????? TSP, TST IP236?? like_domain
#4 E001237??????????????? TSV IP237 simple_domain
A.K.
----- Original Message -----
From: tgodoy <tingola_07 at hotmail.com>
To: r-help at r-project.org
Cc:
Sent: Friday, August 3, 2012 1:00 PM
Subject: [R] How to concatenate a several rows according with a column ?
Hi, I'm a new user or R and I try to concatenate a several rows according
with the value in a column.
this is my data.frame and I want to concatenate my data.frame according with
the column "b" and make a new data.frame with the information in the
others
columns.
>table1
? ? ? ? a? ? ? ? ? ? ? ? ? ? b? ? ? ? c? ? ? ? ? ? d
1? ? ? E001234? ? ? TSA? ? IP234? like_domain
2? ? ? E001234? ? ? TSB? ? IP234? like_domain? ?
3? ? ? E001234? ? ? TSC? ? IP234? like_domain
4? ? ? E001234? ? ? TSD? ? IP234? like_domain
5? ? ? E001235? ? ? TSA? ? IP235? two_domain
6? ? ? E001235? ? ? TSD? ? IP235? two_domain
7? ? ? E001235? ? ? TSS? ? IP235? two_domain
8? ? ? E001236? ? ? TSP? ? IP236? like_domain
9? ? ? E001236? ? ? TST? ? IP236? like_domain
10? ? E001237? ? ? TSV? ? IP237? simple_domain
I want my table in this way
>table_new
? ? ? ? a? ? ? ? ? ? ? ? ? ? b? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? c? ? ? ? ?
?
d
1? ? ? E001234? ? ? TSA, TSB, TSC, TSD? ? IP234? like_domain
2? ? ? E001235? ? ? ? ? ? ? ? TSA, TSD, TSS? ? IP235? two_domain
3? ? ? E001236? ? ? ? ? ? ? ? TSP, TSP, TST? ? IP236? like_domain
4? ? ? E001237? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TSV? ? IP237?
simple_domain
How can I do this in R?
Thanks
--
View this message in context:
http://r.789695.n4.nabble.com/How-to-concatenate-a-several-rows-according-with-a-column-tp4639072.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
David Winsemius
2012-Aug-04 08:34 UTC
[R] How to concatenate a several rows according with a column ?
On Aug 3, 2012, at 10:00 AM, tgodoy wrote:> Hi, I'm a new user or R and I try to concatenate a several rows > according > with the value in a column. > > this is my data.frame and I want to concatenate my data.frame > according with > the column "b" and make a new data.frame with the information in the > others > columns. >table1 <- <-read.table(text=" a b c d 1 E001234 TSA IP234 like_domain 2 E001234 TSB IP234 like_domain 3 E001234 TSC IP234 like_domain 4 E001234 TSD IP234 like_domain 5 E001235 TSA IP235 two_domain 6 E001235 TSD IP235 two_domain 7 E001235 TSS IP235 two_domain 8 E001236 TSP IP236 like_domain 9 E001236 TST IP236 like_domain 10 E001237 TSV IP237 simple_domain",header=TRUE,stringsAsFactors=FALSE) > aggrdat <- with(table1, aggregate(b, list(a,c,d), FUN=paste, sep=",") ) > names(aggrdat) <- names(table1)[c(2:4,1)] > aggrdat b c d a 1 E001234 IP234 like_domain TSA, TSB, TSC, TSD 2 E001236 IP236 like_domain TSP, TST 3 E001237 IP237 simple_domain TSV 4 E001235 IP235 two_domain TSA, TSD, TSS Swapping the column position is left as an exercise. -- David.> > I want my table in this way > >> table_new > a b c > d > 1 E001234 TSA, TSB, TSC, TSD IP234 like_domain > 2 E001235 TSA, TSD, TSS IP235 two_domain > 3 E001236 TSP, TSP, TST IP236 like_domain > 4 E001237 TSV IP237 > simple_domain > > How can I do this in R? > > Thanks > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-concatenate-a-several-rows-according-with-a-column-tp4639072.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD Alameda, CA, USA