Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

removing columns in csv format in R language

$
0
0

I have the following table in csv format:

I have the following gene information in a csv format table:

                    1       3       1       2     2         3
1415670_at  1   365.1   293.4   288.9   394.5   312     381.6
1415671_at  2   556.1   584.2   567.8   592.8   471.6   513.1
1415672_at  3   1048.3  763.1   1074.9  852.3   826.1   898.3
1415673_at  4   60.8    51.7    51.6    224     248.4   150.7
1415674_at  5   129.1   107.2   230.4   175.5   250.5   172.4

As you can see I got some columns labeled with 1,2 and 3. I have made a VB script that deletes the columns that are different from 1 and 2 in Excel. The question that I have is how I can do that using only R? So that my resulting table will be:

                    1   1       2          2        
1415670_at  1   365.1   293.4   394.5     312       
1415671_at  2   556.1   584.2   592.8   471.6   
1415672_at  3   1048.3  763.1   852.3   826.1   
1415673_at  4   60.8    51.7    224     248.4   
1415674_at  5   129.1   107.2   175.5   250.5   

By the way, this is only an example, I can have other columns labeled 4, 5 and 6, but I only want to keep those that are labeled 1 and 2

I have tried the solution posted, that is to use:

m<-read.csv("test1.csv")
smallerdat <- m[ grep("^X1$|^X2$|X1\\.|X2\\." , names(m) ) ]

where m is the table in csv format, but the results that I got is:

    X1  X1.1        X2      X2.2        
365.1   293.4   394.5     312       
556.1   584.2   592.8   471.6   
1048.3  763.1   852.3   826.1   
60.8    51.7    224     248.4   
129.1   107.2   175.5   250.5

So it is deleting the first two columns that I need those. How not to delete those columns? and also how to keep the original format, I mean only 1 and 2 in the header and not those Xs


Viewing all articles
Browse latest Browse all 201839

Trending Articles