mardi 28 janvier 2020

R special if- statement over column names

I have a data frame and I want to add the units (µg,g,...)to the values of the columnnames. My data is called BLS_datatable. I have added the units seperately with the following code. I know that is a bit redundant, but it works. The value here is KiloJoule.

colnames(BLS_datatable) <- gsub(colnames(BLS_datatable), pattern = 'KiloJoule', replacement = 'KiloJoule[kj]')

mg is the most common unit in my data frame that consists of more than 100 columns. I want to do a "if statement" for adding the unit mg to the suitable values. Unfortunately, this is not working:

if("[" %in% colnames(BLS_datatable)){
  print(colnames(BLS_datatable))
} else {
  print(colnames(c(BLS_datatable),"[mg]"))
}

What is wrong here? Thank you for helping me ,Cheers

Aucun commentaire:

Enregistrer un commentaire