pdubois
pdubois

Reputation: 7790

Combining first two columns and turn it into row names in R data.frame

I have a data that looks like this:

> read.table("http://dpaste.com/1491018/plain/",header=TRUE)
        Probes Gene.symbol Stim1 Stim2 Stim3 Stim4
1   1415670_at        Copg 1.133 1.049 1.013 1.124
2   1415671_at    Atp6v0d1 1.068 1.006 1.082 1.234
3   1415672_at      Golga7 1.010 0.883 1.061 1.029
4   1415673_at        Psph 0.943 0.799 0.982 1.064
5 1415674_a_at     Trappc4 1.048 0.960 1.191 1.118
6   1415675_at        Dpm2 1.053 1.104 1.053 1.057

What I want to do is to create a new data frame that combines first two column and turn it into row names,

                     Stim1 Stim2 Stim3 Stim4
1415670_at-Copg      1.133 1.049 1.013 1.124
1415671_at-Atp6v0d1  1.068 1.006 1.082 1.234
1415672_at-Golga7    1.010 0.883 1.061 1.029
1415673_at-Psph      0.943 0.799 0.982 1.064
1415674_a_at-Trappc4 1.048 0.960 1.191 1.118
1415675_at-Dpm2      1.053 1.104 1.053 1.057

How can this be done?

Upvotes: 5

Views: 3907

Answers (2)

Bárbara Seaman
Bárbara Seaman

Reputation: 21

3 years later, but it is very simple.

row.names(http://dpaste.com/1491018/plain/)<-paste(Probes,Gene.symbol, sep="-")

Upvotes: 2

neversaint
neversaint

Reputation: 63994

Try this:

 dat <- read.table("http://dpaste.com/1491018/plain/",header=TRUE)
 rownames(dat) <- do.call(paste,c(dat[c("Probes","Gene.symbol")],sep="-"))
 dat <- dat[,!names(dat) %in% c("Probes","Gene.symbol")] 
 dat

Result:

>      dat
                     Stim1 Stim2 Stim3 Stim4
1415670_at-Copg      1.133 1.049 1.013 1.124
1415671_at-Atp6v0d1  1.068 1.006 1.082 1.234
1415672_at-Golga7    1.010 0.883 1.061 1.029
1415673_at-Psph      0.943 0.799 0.982 1.064
1415674_a_at-Trappc4 1.048 0.960 1.191 1.118
1415675_at-Dpm2      1.053 1.104 1.053 1.057

Upvotes: 8

Related Questions