0

I'm trying to write a code snippet that will allow others to copy and paste it with minimal changes. There is a variable name that will be different every time, so I would like the other users to be able to assign the variable name to a variable at the head and then I will reference that variable using get() or assign(). This variable will be a data frame. For instance:

name_of_frame <- "number_one"
# This is where they will name the variable

assign (name_of_frame, read.csv("....", header=FALSE))

new_variable <- get(name_of_frame) [2:nrow(get(name_of_frame)),]

These work fine. The one that doesn't is this:

colnames(get(name_of_frame)) <- new_vector

This throws the error: Error in colnames(get(name_of_frame)) <- new_vector : could not find function "get<-"

If I substitute this it works fine:

colnames(number_one) <- new_vector 

Why is this the only context where I can't use get(name of variable) to substitute for the variable?

leviemb
  • 33
  • 5

0 Answers0