Search code examples
r

Converting colnames from character to numeric when extracting them from a dataframe


I have what I think is a basic R question. I have a dataset that looks similar to the example below

exampledf <- data.frame(Chr3_2 = c(902, 1935, 2469, 3478),
                        Chr1_2 = c(1565, 4123, 6278, 8567),
                        Chr3_1 = c(891, 1249, 2578, 3698),                        
                        Chr2_1 = c(595, 2589, 3587, 5697),
                        Chr1_1 = c(1225, 4385, 6498, 8486),                        
                        Chr2_2 = c(563, 2436, 3674, 5210),
                        scaffold4 = c(NA, NA, 201, 367),
                        scaffold2 = c(103, NA, NA, 203))
print(exampledf)

#> print(exampledf)
#  Chr1_1 Chr1_2 Chr2_1 Chr2_2 Chr3_1 Chr3_2 scaffold4 scaffold2
#1   1225   1565    595    563    891    902        NA       103
#2   4385   4123   2589   2436   1249   1935        NA        NA
#3   6498   6278   3587   3674   2578   2469       201        NA
#4   8486   8567   5697   5210   3698   3478       367       203

How do I convert the column names from character to numeric when I extract them from the dataframe? Using as.numeric only returns NA for column names.

znum <- as.numeric(colnames(exampledf)[1:6])
znum

#> znum
#[1] NA NA NA NA NA NA

I need to be able to assign a numeric value (in order) to each column, in a way that I can keep track of the change so I know for example that Chr1_1=1, Chr1_2=2, etc. and Chr3_2=6. In the dataframe these column values aren't in order per say. Expected output should be:

c(1,2,3,4,5,6)

Solution

  • Create a data frame for the purpose

    column_lookup = data.frame(
      number = seq_along(exampledf), 
      name = names(exampledf)
    )
    
    column_lookup
    #   number      name
    # 1      1    Chr3_2
    # 2      2    Chr1_2
    # 3      3    Chr3_1
    # 4      4    Chr2_1
    # 5      5    Chr1_1
    # 6      6    Chr2_2
    # 7      7 scaffold4
    # 8      8 scaffold2