I'm an R newbie. I exported data from a database and am trying to rename the columns.
Example existing names (one site per water quality parameter) are in quotes below. There are the 6 possible parameters at each site and 40 sites; I would like to rename columns based on parameter and site. Site names are 3-7 characters and always occur after the last decimal point. My dataset (AQexport1) has 240 columns and 47,714 rows (rows are time stamps of hourly continuous data). I want to be able to use the code for other exports from this database with same format and parameters, but possibly different sites.
For example:
- "Water.Temp.Water.Temp.BUBU" | "Water.Temp.Temperature.BUBU" <--- Temp.BUBU
- "Water.Temp.Field.Visits.KNF_DUP" <--- FVTemp.KNF_DUP
- "Sp.Cond.TempCorrected_nodrift.LOD_DUP" <---SpCnd.LOD_Dup
- "Sp.Cond.TempCorrected.PFM" <--- SpC.PFM
- "Sp.Cond.Field.Visits.CC7" <-- FVSpC.CC7
- "Cond.Conductivity.TM02Dup"<-- Cond.TM02Dup
I can not figure out how to write the contains() in an if statement (I realize the syntax below is wrong, I'm just trying to show what I'm thinking), or how to extract characters from a string with multiple decimal points and that does not extract the same number of characters from the end of the column name. I am also wondering if a for loop through colnames() is the best solution.
for (i in 1:colnames(AQexport1)){
if (colnames(AQexport1[i]) contains "Water.Temp.W" | "Water.Temp.T"){
colnames(AQexport1[i]) <- Temp.insert_site_name_here
}
elseif (colnames(AQexport1[i])) contains "Water.Temp.F") {
colnames(AQexport1[i]) <- FVTemp.insert_site_name_here
}
elseif (colnames(AQexport1[i])) contains "nodrift") {
colnames(AQexport1[i])<-SpCnd.insert_site_name_here
}
elseif (colnames((AQexport1[i])) contains "Sp.Cond.T") {
colnames(AQexport1[i])<-SpC.insert_site_name_here
}
#continue elseif statements
}