[R] How to concatenate a several rows according with a column ?
Rui Barradas
ruipbarradas at sapo.pt
Sat Aug 4 11:05:23 CEST 2012
Hello,
Inline.
Em 04-08-2012 09:34, David Winsemius escreveu:
>
> 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)]
It's a column order issue, not a names one. Using Jean's form of aggregate,
aggrdat <- aggregate(b ~ a + c + d, data = table1, paste, sep=",")
(table2 <- aggrdat[, c(1, 4, 2, 3)])
Hope this helps,
Rui Barradas
> > 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.
>
More information about the R-help
mailing list