[R] transform 1 col to 2 col

Jim Lemon jim at bitwrit.com.au
Thu May 24 10:43:17 CEST 2012


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



More information about the R-help mailing list