Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: Change column name based on string in row value

I have this df that comes from a massive csv file. The current column names have placeholders for their names. Is there a way to make the name of each column the sting part of the data. The string will be the same for the entire column going down. I would change the names manually, but there are 256 columns.

Starting df:

   col2          col3       col4     ...
1 version=1    numCh=10   reserved=0
2 version=1    numCh=10   reserved=0
3 version=1    numCh=11   reserved=0
4 version=1    numCh=11   reserved=0
...

The desired output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  version    numCh    reserved   ...
1    1        10        0
2    1        10        0
3    1        11        0
4    1        11        0
...

Been trying different methods and seeing if anybody else has created something like this.

>Solution :

Extract the first row, and remove the substring from = with trimws, then loop across the columns, extract the numeric part with parse_number and set the column names with the substring of first row

library(dplyr)
 v1 <- trimws(unlist(df1[1,]), whitespace = "=.*")
df1 %>% 
    mutate(across(everything(), readr::parse_number)) %>% 
     setNames(v1)

-output

  version numCh reserved
1       1    10        0
2       1    10        0
3       1    11        0
4       1    11        0

data

df1 <- structure(list(col2 = c("version=1", "version=1", "version=1", 
"version=1"), col3 = c("numCh=10", "numCh=10", "numCh=11", "numCh=11"
), col4 = c("reserved=0", "reserved=0", "reserved=0", "reserved=0"
)), class = "data.frame", row.names = c("1", "2", "3", "4"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading