[R] union data in column
jim holtman
jholtman at gmail.com
Sat Jul 24 07:36:08 CEST 2010
try this:
> x <- read.table(textConnection(" GENEID col1 col2 col3 col4
+ G234064 1 0 0 0
+ G234064 1 0 0 0
+ G234064 1 0 0 0
+ G234064 0 1 0 0
+ G234065 0 1 0 0
+ G234065 0 1 0 0
+ G234065 0 1 0 0
+ G234065 0 0 1 0
+ G234065 0 0 1 0
+ G234065 0 0 0
1"), header=TRUE, as.is=TRUE)
> closeAllConnections()
> # split the data and process the columns
> do.call(rbind, lapply(split(x, x$GENEID), function(z){
+ # 'or' the columns
+
+ colOR <- t(apply(z[-1], 2, any)) + 0L
+ data.frame(GENEID=z[[1]][1], colOR)
+ }))
GENEID col1 col2 col3 col4
G234064 G234064 1 1 0 0
G234065 G234065 0 1 1 1
>
On Sat, Jul 24, 2010 at 12:10 AM, Fahim Md <fahim.md at gmail.com> wrote:
> Is there any function/way to merge/unite the following data
>
> GENEID col1 col2 col3 col4
> G234064 1 0 0 0
> G234064 1 0 0 0
> G234064 1 0 0 0
> G234064 0 1 0 0
> G234065 0 1 0 0
> G234065 0 1 0 0
> G234065 0 1 0 0
> G234065 0 0 1 0
> G234065 0 0 1 0
> G234065 0 0 0 1
>
>
> into
> GENEID col1 col2 col3 col4
> G234064 1 1 0 0
> // 1 appears in col1 and col2 above, rest are zero
> G234065 0 1 1 1
> // 1 appears in col2 , 3 and 4 above.
>
>
> Thanks
>
>
>
> --
> Fahim
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list