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?
The rownames() method in R is used to assign row names to the dataframe. It is assigned using a character vector consisting of desired names with a length equivalent to the number of rows in dataframe. We can simply assign it to any column of the dataframe if it contains all unique values.
How do I concatenate two columns in R? To concatenate two columns you can use the <code>paste()</code> function. For example, if you want to combine the two columns A and B in the dataframe df you can use the following code: <code>df['AB'] <- paste(df$A, df$B)</code>.
Convert multiple columns into a single column, To combine numerous data frame columns into one column, use the union() function from the tidyr package.
To convert a column values to column names, we can use dcast function of reshape2 package. For example, if we have a data frame called df that contains two columns say x and y, where x is categorical and y is numerical. Now if we want to convert the categories in x as column names then it can be done as dcast(df,y~x).
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
3 years later, but it is very simple.
row.names(http://dpaste.com/1491018/plain/)<-paste(Probes,Gene.symbol, sep="-")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With