I come across this problem regularly and I would like to know if there's a more elegant, less redundant way to do this.
For example, say I read x .csv files and I want to remove the first column from each one. I can remove it individually with x[,-1] several times, I can create a function:
rmx <- function(x){
x[,-1]
}
x <- rmx(x)
But I would still have to type that out x amount of times, or I can store these data frames in a list and pass them through a loop or a function like sapply, but then how do I unlist them so that they return to variables in my global environment so I can continue working with them individually? Or is there another way to do this?
Just how to construct the NewName variable will depend on where the files are stored and whether you have list.files() return the full path or not. I kept it simple by reading files in my working directory. The ReturnedNames variable serves no purpose other than catching the useless return value of the function. The walk() function from purrr would do this more elegantly.
Amazing! This is exactly what I was looking for. Thank you so much for that function, I'm certain I will be using it regularly. Could you also tell me what the "~" syntax does? I've Googled it before and haven't found an answer.
in this context the tilde is syntax that purrr/tidyverse requires in order to enable us to use shortcut syntax. i.e. within the curly brackets after the tilde, the .x is understood to represent the item from list_to_work that is currently being processed. purrr::map is using ~ (which in R defined a formula) and knows how to interpret it, to turn it into an anonymous function for us, in this case its a function that gets an object from the global environment by its name, and performs action_to_do() on it.