My goal is to set the column names on a data frame. The name of this data frame is stored in a variable name_of_table.
name_of_table<-"table_13"
assign(name_of_table,read.csv("table_13_air_vehicle_risks_likelihood_and_cost_effects.csv", header=FALSE))
# This works fine, like table_13 <- read.csv(...)
first_level_header <- c("one","two","three","four","five")
colnames(get(name_of_table)) <- first_level_header
# Throws error:
#Error in colnames(get(name_of_table)) <- first_level_header :
# could not find function "get<-"
Obviously if I substitute table_13 for get(name_of_table) this works. If instead I try:
colnames(names(name_of_table)) <- first_level_header
#Throws error: Error in `colnames<-`(`*tmp*`, value = c("one", "two", "three", "four", : attempt to set
#'colnames' on an object with less than two dimensions
I was pointed to this post earlier: R using get() inside colnames
But eval(parse(paste0("colnames(",name_of_table,")<- first_level_header"))), besides being hideous, does not work either: Error in file(filename, "r") : cannot open the connection
I don't understand the suggestion involving SetNames.
I apologize if get/assign is not the right approach, of course I want to do this the "right" way, I appreciate the guidance.