3

I have a set of data like so

  GSM482075 GSM482076 GSM482077 GSM482078 GSM482079 GSM482080 GSM482081 GSM482082
1   8.79703   8.04291   7.53674   9.94527   8.38775   8.02375   11.1607   10.5481
2   5.66556   6.42794   6.62527   6.19131   5.34138   5.69543    5.9252   5.55675

I want to transpose and turn the columns into rows and rows into columns but when I use t() it converts the numeric values into strings.

 1         2        
GSM482075 "8.79703" "5.66556"
GSM482076 "8.04291" "6.42794"
GSM482077 "7.53674" "6.62527"
GSM482078 "9.94527" "6.19131"
GSM482079 "8.38775" "5.34138"
GSM482080 "8.02375" "5.69543"
GSM482081 "11.1607" "5.9252" 
GSM482082 "10.5481" "5.55675"

I've looked around but I haven't found a straightforward way to keep the numbers numeric. I was thinking about stripping out the labels but I haven't seen any answers on how to do that except people telling others not to. Any suggestions? thanks

EDIT: Results of str(geomatrixsub) 1st object shown

 'data.frame':  2 obs. of  8 variables:
$ GSM482075: chr  "8.79703" "5.66556"
$ GSM482076: chr  "8.04291" "6.42794"
$ GSM482077: chr  "7.53674" "6.62527"
$ GSM482078: chr  "9.94527" "6.19131"
$ GSM482079: chr  "8.38775" "5.34138"
$ GSM482080: chr  "8.02375" "5.69543"
$ GSM482081: chr  "11.1607" "5.9252"
$ GSM482082: chr  "10.5481" "5.55675"

using t(geomatrixsub[1:2,] )

      1         2        
GSM482075 "8.79703" "5.66556"
GSM482076 "8.04291" "6.42794"
GSM482077 "7.53674" "6.62527"
GSM482078 "9.94527" "6.19131"
GSM482079 "8.38775" "5.34138"
GSM482080 "8.02375" "5.69543"
GSM482081 "11.1607" "5.9252" 
GSM482082 "10.5481" "5.55675"
A D
  • 195
  • 5
  • 20

2 Answers2

2

I know there are several solutions around here with reshape2 and tidyr functions. I've tried a base R solution. Here is it:

 t(dat[1:2, ])
                 1       2
GSM482075  8.79703 5.66556
GSM482076  8.04291 6.42794
GSM482077  7.53674 6.62527
GSM482078  9.94527 6.19131
GSM482079  8.38775 5.34138
GSM482080  8.02375 5.69543
GSM482081 11.16070 5.92520
GSM482082 10.54810 5.55675

I have used data provided, read in R with this with this code that has a data frame of numeric but with matrixes I think it should work too:

dat <- read.table(text = " GSM482075 GSM482076 GSM482077 GSM482078 GSM482079 GSM482080 GSM482081 GSM482082
1   8.79703   8.04291   7.53674   9.94527   8.38775   8.02375   11.1607   10.5481
                  2   5.66556   6.42794   6.62527   6.19131   5.34138   5.69543    5.9252   5.55675")
SabDeM
  • 7,050
  • 2
  • 25
  • 38
0

I know that this is an old post, but I had the same problem and I found another way to do it.

dat <- read.table(text = " GSM482075 GSM482076 GSM482077 GSM482078 GSM482079 GSM482080 GSM482081 GSM482082
1   8.79703   8.04291   7.53674   9.94527   8.38775   8.02375   11.1607   10.5481
                  2   5.66556   6.42794   6.62527   6.19131   5.34138   5.69543    5.9252   5.55675")

dat_t = as.data.frame(x = t(dat), stringsAsFactors = FALSE)


> dat_t
                 1       2
GSM482075  8.79703 5.66556
GSM482076  8.04291 6.42794
GSM482077  7.53674 6.62527
GSM482078  9.94527 6.19131
GSM482079  8.38775 5.34138
GSM482080  8.02375 5.69543
GSM482081 11.16070 5.92520
GSM482082 10.54810 5.55675

The original source is here: https://community.rstudio.com/t/how-to-transpose-data-frame-and-retain-variable-data-types/45843

emr2
  • 1,436
  • 7
  • 23