Dear All How can I transform 1 column to 2 columns in R? 211217_s_at GO:0005249 211217_s_at GO:0005251 211217_s_at GO:0005515 211217_s_at GO:0015271 211217_s_at GO:0030955 211217_s_at GO:0005249 211217_s_at GO:0005251 211217_s_at GO:0005515 211217_s_at GO:0015271 211217_s_at GO:0030955 Best Wishes, Soheila [[alternative HTML version deleted]]
On 05/24/2012 05:10 PM, Soheila Khodakarim wrote:> Dear All > > How can I transform 1 column to 2 columns in R? > > 211217_s_at > > GO:0005249 > > 211217_s_at > > GO:0005251 > > 211217_s_at > > GO:0005515 > > 211217_s_at > > GO:0015271 > > 211217_s_at > > GO:0030955 > > > > > > 211217_s_at > > GO:0005249 > > 211217_s_at > > GO:0005251 > > 211217_s_at > > GO:0005515 > > 211217_s_at > > GO:0015271 > > 211217_s_at > > GO:0030955 >Hi Soheila, Let us begin with the assumption that your "col" is roughly what you posted, and that text resides in a file named "inscrutable.dat". Let us first read that text into a data frame in R: inscrutatble.df<-read.table("inscrutable.dat") inscrutable.df V1 1 211217_s_at 2 GO:0005249 3 211217_s_at 4 GO:0005251 5 211217_s_at 6 GO:0005515 7 211217_s_at 8 GO:0015271 9 211217_s_at 10 GO:0030955 11 211217_s_at 12 GO:0005249 13 211217_s_at 14 GO:0005251 15 211217_s_at 16 GO:0005515 17 211217_s_at 18 GO:0015271 19 211217_s_at 20 GO:0030955 Now we will further assume that you want to place every odd element in one "col" and every even element in the other "col". inscrutable2.df<-data.frame( col1=inscrutable.df$V1[seq(1,length(inscrutable.df$V1),by=2)], col2=inscrutable.df$V1[seq(2,length(inscrutable.df$V1),by=2)]) inscrutable2.df col1 col2 1 211217_s_at GO:0005249 2 211217_s_at GO:0005251 3 211217_s_at GO:0005515 4 211217_s_at GO:0015271 5 211217_s_at GO:0030955 6 211217_s_at GO:0005249 7 211217_s_at GO:0005251 8 211217_s_at GO:0005515 9 211217_s_at GO:0015271 10 211217_s_at GO:0030955 There we are - easy as pie. Jim
See insertion below -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 5/24/12 1:43 AM, "Jim Lemon" <jim at bitwrit.com.au> wrote:>On 05/24/2012 05:10 PM, Soheila Khodakarim wrote: >> Dear All >> >> How can I transform 1 column to 2 columns in R? >> >> 211217_s_at >> >> GO:0005249 >> >> 211217_s_at >> >> GO:0005251 >> >> 211217_s_at >> >> GO:0005515 >> >> 211217_s_at >> >> GO:0015271 >> >> 211217_s_at >> >> GO:0030955 >> >> >> >> >> >> 211217_s_at >> >> GO:0005249 >> >> 211217_s_at >> >> GO:0005251 >> >> 211217_s_at >> >> GO:0005515 >> >> 211217_s_at >> >> GO:0015271 >> >> 211217_s_at >> >> GO:0030955 >> >Hi Soheila, >Let us begin with the assumption that your "col" is roughly what you >posted, and that text resides in a file named "inscrutable.dat". Let us >first read that text into a data frame in R: > >inscrutatble.df<-read.table("inscrutable.dat") > inscrutable.df > V1 > >1 211217_s_at >2 GO:0005249 >3 211217_s_at >4 GO:0005251 >5 211217_s_at >6 GO:0005515 >7 211217_s_at >8 GO:0015271 >9 211217_s_at >10 GO:0030955 >11 211217_s_at >12 GO:0005249 >13 211217_s_at >14 GO:0005251 >15 211217_s_at >16 GO:0005515 >17 211217_s_at >18 GO:0015271 >19 211217_s_at >20 GO:0030955 > >Now we will further assume that you want to place every odd element in >one "col" and every even element in the other "col".Or perhaps: matrix(inscrutable.df$V1, ncol=2, byrow=TRUE) or data.frame(matrix(inscrutable.df$V1, ncol=2, byrow=TRUE)) to get a data frame. -Don> >inscrutable2.df<-data.frame( > col1=inscrutable.df$V1[seq(1,length(inscrutable.df$V1),by=2)], > col2=inscrutable.df$V1[seq(2,length(inscrutable.df$V1),by=2)]) >inscrutable2.df > col1 col2 >1 211217_s_at GO:0005249 >2 211217_s_at GO:0005251 >3 211217_s_at GO:0005515 >4 211217_s_at GO:0015271 >5 211217_s_at GO:0030955 >6 211217_s_at GO:0005249 >7 211217_s_at GO:0005251 >8 211217_s_at GO:0005515 >9 211217_s_at GO:0015271 >10 211217_s_at GO:0030955 > >There we are - easy as pie. > >Jim > >______________________________________________ >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.
Hi, Just an addendum. Suppose if you want to add more columns and do the split, you can try, inscrutable.df2<-data.frame(inscrutable.df1,V2=(c(rnorm(1),"GTDF","SQ:1234","DFFD","DFDSE")[rep(c(1,2,3,4,5),times=4)])) inscrutable3.df3<-data.frame(matrix(inscrutable.df1$V1,ncol=2,byrow=TRUE),matrix(inscrutable.df2$V2,ncol=2,byrow=TRUE))?? A.K. ----- Original Message ----- From: "MacQueen, Don" <macqueen1 at llnl.gov> To: Soheila Khodakarim <lkhodakarim at gmail.com> Cc: "r-help at r-project.org" <r-help at r-project.org> Sent: Thursday, May 24, 2012 10:19 AM Subject: Re: [R] transform 1 col to 2 col See insertion below -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 5/24/12 1:43 AM, "Jim Lemon" <jim at bitwrit.com.au> wrote:>On 05/24/2012 05:10 PM, Soheila Khodakarim wrote: >> Dear All >> >> How can I transform 1 column to 2 columns in R? >> >> 211217_s_at >> >> GO:0005249 >> >> 211217_s_at >> >> GO:0005251 >> >> 211217_s_at >> >>? ? GO:0005515 >> >> 211217_s_at >> >> GO:0015271 >> >>? ? 211217_s_at >> >>? ? GO:0030955 >> >> >> >> >> >> 211217_s_at >> >> GO:0005249 >> >> 211217_s_at >> >> GO:0005251 >> >>? ? 211217_s_at >> >>? ? GO:0005515 >> >> 211217_s_at >> >> GO:0015271 >> >>? ? 211217_s_at >> >> GO:0030955 >> >Hi Soheila, >Let us begin with the assumption that your "col" is roughly what you >posted, and that text resides in a file named "inscrutable.dat". Let us >first read that text into a data frame in R: > >inscrutatble.df<-read.table("inscrutable.dat") >? inscrutable.df >? ? ? ? ? ? V1 > >1 211217_s_at >2? GO:0005249 >3? 211217_s_at >4? GO:0005251 >5? 211217_s_at >6? GO:0005515 >7? 211217_s_at >8? GO:0015271 >9? 211217_s_at >10? GO:0030955 >11 211217_s_at >12? GO:0005249 >13 211217_s_at >14? GO:0005251 >15 211217_s_at >16? GO:0005515 >17 211217_s_at >18? GO:0015271 >19 211217_s_at >20? GO:0030955 > >Now we will further assume that you want to place every odd element in >one "col" and every even element in the other "col".Or perhaps: ? matrix(inscrutable.df$V1, ncol=2, byrow=TRUE) or ? data.frame(matrix(inscrutable.df$V1, ncol=2, byrow=TRUE)) to get a data frame. -Don> >inscrutable2.df<-data.frame( >? col1=inscrutable.df$V1[seq(1,length(inscrutable.df$V1),by=2)], >? col2=inscrutable.df$V1[seq(2,length(inscrutable.df$V1),by=2)]) >inscrutable2.df >? ? ? ? ? col1? ? ? col2 >1? 211217_s_at GO:0005249 >2? 211217_s_at GO:0005251 >3? 211217_s_at GO:0005515 >4? 211217_s_at GO:0015271 >5? 211217_s_at GO:0030955 >6? 211217_s_at GO:0005249 >7? 211217_s_at GO:0005251 >8? 211217_s_at GO:0005515 >9? 211217_s_at GO:0015271 >10 211217_s_at GO:0030955 > >There we are - easy as pie. > >Jim > >______________________________________________ >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.______________________________________________ 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.