MonkeyCousin
MonkeyCousin

Reputation: 189

column names in R - substitute part of name string

I have X745.971008.Nanometers as col names and can't figure out how to make it in the format:

I want col names thus: 745.971008_nm

or better, with the wavelength rounded to 3 dp: 745.971_nm

I have tried: names(df) <- sub('X\\.+(\\d+\\.\\d+)\\.Nanometers\\.', '\\1_nm', names(df))

and: colnames(df) <- gsub("X(.+).Nanometers.", "\\1_nm", colnames(df))

Thanks heaps

Upvotes: 1

Views: 164

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388862

You can use sub :

x <- 'X745.971008.Nanometers'
sub('X(\\d+\\.\\d+).Nanometers', '\\1_nm', x)
#[1] "745.971008_nm"

In case of column names try :

colnames(df) <- sub('X(\\d+\\.\\d+).Nanometers', '\\1_nm', colnames(df))

If you want to round the values :

paste0(round(as.numeric(sub('X(\\d+\\.\\d+).Nanometers', '\\1',x)), 3), '_nm')
#[1] "745.971_nm"

Upvotes: 0

Eric
Eric

Reputation: 2849

To accomplish this, try using rename_all and str_replace_all.

I guess this would be the most accurate way since it rounds the values in the variable names like you requested:

df <- data.frame(
  'X745.971008.Nanometers' = c(1, 2, 3, 4, 5),
  'X743.971999.Nanometers' = c(1, 2, 3, 4, 5)
)

library(dplyr)
library(stringr)

df %>% 
  stack() %>% 
  mutate(ind = str_replace(ind, 'X', ''),
         ind = str_replace(ind, '.Nanometers', ''),
         ind = paste(round(as.numeric(ind), digits = 3), '_nm')) %>%  
  unstack(df2, form = values~ind) %>% 
  rename_all(
    funs(
      stringr::str_replace_all(., 'X', '')
    )
  ) %>% 
  rename_all(
    funs(
      stringr::str_replace_all(., '._', '_')
    )
  )

#>   743.972_nm 745.971_nm
#> 1          1          1
#> 2          2          2
#> 3          3          3
#> 4          4          4
#> 5          5          5

Created on 2021-03-18 by the reprex package (v0.3.0)

OR

df <- data.frame(
  'X745.971008.Nanometers' = c(1, 2, 3, 4, 5),
  'X743.971999.Nanometers' = c(1, 2, 3, 4, 5)
)

library(dplyr)
library(stringr)

df %>% 
  rename_all(
    funs(
      stringr::str_replace_all(., 'X', '')
    )
  ) %>% 
  rename_all(
    funs(
      stringr::str_replace_all(., '.Nanometers', '_nm')
    )
  )

#>   745.971008_nm 743.971999_nm
#> 1             1             1
#> 2             2             2
#> 3             3             3
#> 4             4             4
#> 5             5             5

Created on 2021-03-18 by the reprex package (v0.3.0)

OR

df <- data.frame(
  'X745.971008.Nanometers' = c(1, 2, 3, 4, 5),
  'X743.971999.Nanometers' = c(1, 2, 3, 4, 5)
)

library(dplyr)
library(stringr)

df %>% 
  rename_all(
    funs(
      stringr::str_replace_all(., 'X', '')
    )
  ) %>% 
  rename_all(
    funs(
      stringr::str_replace_all(., '\\d\\d\\d.Nanometers', '_nm')
    )
  )

#>   745.971_nm 743.971_nm
#> 1          1          1
#> 2          2          2
#> 3          3          3
#> 4          4          4
#> 5          5          5

Created on 2021-03-18 by the reprex package (v0.3.0)

Upvotes: 2

Related Questions