Re: [R] Split column

2009-11-24 Thread Lisaj
I really appreciate your help. But if there is a big dataset, I need to write lots of code. Lisa David Winsemius wrote: > > The solution I offered does exactly that. It also addresses your other > supplemental request. > > -- > David (Dangerfield?) > > On Nov 24, 2009, at 5:57 PM, Lisaj

Re: [R] Split column

2009-11-24 Thread David Winsemius
The solution I offered does exactly that. It also addresses your other supplemental request. -- David (Dangerfield?) On Nov 24, 2009, at 5:57 PM, Lisaj wrote: I have a further question. If there is NA (missing data) in the original dataset, just like this: id var1 var2 1 1

Re: [R] Split column

2009-11-24 Thread Lisaj
I have a further question. If there is NA (missing data) in the original dataset, just like this: id var1 var2 1 1 3 2 3 1 3 NA1 4 1 2 5 2 3 how to deal with it? The converted dataset should be this: id var1.1 var1.2

Re: [R] Split column

2009-11-24 Thread Lisaj
Thank you for your help. But how to change the column names to var1.1, var1.2, var2.1, and var2.2? Lisa Henrique Dallazuanna wrote: > > Try this: > > cbind(x$id, t(do.call(rbind, lapply(x[-1], sapply, switch, '1' = > c(1,1), '2' = c(1, 2), '3' = c(2, 2) > > On Tue, Nov 24, 2009 at 1:51

Re: [R] Split column

2009-11-24 Thread David Winsemius
On Nov 24, 2009, at 12:53 PM, Adaikalavan wrote: Not very elegant but this does the trick: df <- cbind( var1=c(1,3,2,1,2), var2=c(3,1,1,2,3) ) df <- data.frame(id=1:5, df) data.frame(id=df$id, var1.1 = (df$var1<3) + 2*(df$var1==3), # 1 for < 3; 2 for 3 var1.2 = (d

Re: [R] Split column

2009-11-24 Thread Adaikalavan Ramasamy
Not very elegant but this does the trick: df <- cbind( var1=c(1,3,2,1,2), var2=c(3,1,1,2,3) ) out <- df out[ which(df==1, arr.ind=T) ] <- "1&1" out[ which(df==2, arr.ind=T) ] <- "1&2" out[ which(df==3, arr.ind=T) ] <- "2&2" outlist <- apply(out, 2, strsplit, split="&") do.call( "cbind.data.fram

Re: [R] Split column

2009-11-24 Thread Henrique Dallazuanna
Try this: cbind(x$id, t(do.call(rbind, lapply(x[-1], sapply, switch, '1' = c(1,1), '2' = c(1, 2), '3' = c(2, 2) On Tue, Nov 24, 2009 at 1:51 PM, Lisaj wrote: > > Hello, R users, > > I have a dataset that looks like this: > > id   var1   var2 >  1      1      3 >  2      3      1 >  3      2

[R] Split column

2009-11-24 Thread Lisaj
Hello, R users, I have a dataset that looks like this: id var1 var2 1 1 3 2 3 1 3 2 1 4 1 2 5 2 3 I want to split one column to two columns with 1 = 1 and 1, 2 = 1 and 2, 3 = 2 and 2: id var1.1 var1.2 var2.1 va