0

I have the following data.frame:

structure(list(chr = c("1A", "1A", "1A", "1A", "1A", "1A"), locus = 
c("1145442", 
"1158042", "1158055", "1229616", "1236254", "1238114"), `ES1-5` = 
c(0.0261333333333333, 
0.0928666666666667, 0.0490166666666667, 0.0499666666666667, 
0.0758333333333333, 
0.0514666666666667), `ES6-13` = c(0.0323333333333333, 0.0387666666666667, 
0.0118166666666667, 0.000466666666666671, 0.0800333333333333, 
0.0120666666666667), `ES14-26` = c(0.0702333333333333, 0.0755666666666667, 
-0.0157833333333333, -0.0187333333333333, 0.123633333333333, 
-0.0207333333333333), `ES27-38` = c(-0.132766666666667, -0.120033333333333, 
0.000516666666666665, 0.00836666666666666, -0.112766666666667, 
0.00126666666666667), `SA1-13` = c(-0.00586666666666669, 
-0.0511333333333333, 
-0.0313833333333333, -0.0294333333333333, -0.0138666666666667, 
-0.0297333333333333), `SA14-25` = c(0.00993333333333335, 
-0.0360333333333333, 
-0.0141833333333333, -0.0106333333333333, -0.152866666666667, 
-0.0143333333333333)), row.names = c(NA, 6L), class = "data.frame")

It looks like that :

 chr   locus      ES1-5       ES6-13     ES14-26       ES27-38       SA1-13     SA14-25
 1A 1145442 0.02613333 0.0323333333  0.07023333 -0.1327666667 -0.005866667  0.009933333
 1A 1158042 0.09286667 0.0387666667  0.07556667 -0.1200333333 -0.051133333 -0.036033333
 1A 1158055 0.04901667 0.0118166667 -0.01578333  0.0005166667 -0.031383333 -0.014183333
 1A 1229616 0.04996667 0.0004666667 -0.01873333  0.0083666667 -0.029433333 -0.010633333
 1A 1236254 0.07583333 0.0800333333  0.12363333 -0.1127666667 -0.013866667 -0.152866667
 1A 1238114 0.05146667 0.0120666667 -0.02073333  0.0012666667 -0.029733333 -0.014333333

I would like to transform the column names colnames[3:8] 'ES1-5' 'ES6-13' 'ES14-26' 'ES27-38' 'SA1-13' 'SA14-25' into a factor with the respective levels that arise from the character difference of the character vector colnames[3:8]. I have attempted things like transposing the data.frame to get the character vector into a single column but then the df$chr df$locus columns are not in the right order anymore.

Could someone help me with this issue ? my ultimate goal is to group the levels of this factor when plotting with the lattice package. Please note that as the values of each column in colnames[3:8] are numeric I don't want them to be levels but rather to stay numeric.

moth
  • 1,833
  • 12
  • 29

1 Answers1

1

I would use tidyverse to move from wide to long. I also included a possible histogram from lattice as an example

library(tidyverse)
##I name your original data frame df
x <- df %>% gather(factors, value, -chr, -locus) %>%
                    mutate(factors = factor(factors))

##Example plot
lattice::histogram(~x$value|x$factors)

enter image description here

Henry Cyranka
  • 2,970
  • 1
  • 16
  • 21