rdata.tabler-colnames

Replacing elements of column names with associated strings


I have a data.table with automatically generated column names. The names take the form:

N.x.y.z

Where N is a character (literally N), and the remaining variables are integers.

I also have a .csv which associates iterations of x with meaningful strings. As in:

X Name
1 Model
3 Mileage

What I would like to do is regenerate the column names in the format:

N.Name.y.z

I've tried to work along the lines of extracting the column names first, like

thefile = fread('filepath')

xx <- colnames(thefile)

colindex <- read.csv('the other file path')
colindex[,1] <- paste0('N.', colindex[,1], '.') #Converting x to N.x.

I messed around with grepl, replace_at, splitting the string up by '.'


Solution

  • read.table(text = "X    Name
                       1    Model
                       3    Mileage", 
               header = T, stringsAsFactor = FALSE) -> colindex
    
    
    df1 <- data.frame(`N.1.2.3` = c(1,2), `N.3.1.2` = c(6,5), 
                      `N.1.3.1` = c(3, 4), `N.3.2.2` = c(8, 7))
    
    df1
    #>   N.1.2.3 N.3.1.2 N.1.3.1 N.3.2.2
    #> 1       1       6       3       8
    #> 2       2       5       4       7
    
    names_split <-  as.data.frame(strsplit(names(df1), "\\."))
    names_split[2,] <-  colindex[match(names_split[2,], colindex$X), "Name"]
    names(df1) <- apply(names_split, 2, paste, collapse = ".")
    
    df1
    #>   N.Model.2.3 N.Mileage.1.2 N.Model.3.1 N.Mileage.2.2
    #> 1           1             6           3             8
    #> 2           2             5           4             7
    

    Created on 2024-03-18 with reprex v2.0.2