I'm going to explain what my code is supposed to do:
First I import an arbitrary number of dataframes of the same number of columns and somewhat similar names (they're stock tickers).
Second, I create a function that changes the data type of the first column (from factor to date), deletes one column and finally adds an arbitrary number of columns as functions of the other columns (like returns, moving average, etc.)
Finally, since I'm working with plenty of dataframes, I want to apply this said function to all of them through a loop.
Now, as simple as this sounds, I've encountered many issues with the third step.
What I found online was this, to get all names of the dataframes in my environment:
dfs <- ls()[sapply(mget(ls(), .GlobalEnv), is.data.frame)]
Which gives a vector of strings of the dataframes' names. Since these are characters and not the actual dataframe object, I can't loop through them, so I added:
sapply( dfs, function(x) {
get(x) <- formatear(get(x))
})
This maybe was a bad idea, since I'm not familiar with the sapply function.
Now, the sapply function I used returns an error::
Error in get(x) <- formatear(get(x)) :
no se pudo encontrar la función "get<-"
I read that the get(x) function searches for variables that have the name on the vector "x", so I thought I could use it like that, my bad.
Where formatear() is my function in the second step:
formatear <- function(eq){
eq$Volume <- NULL
eq$Date <- as.Date(eq$Date)
nt <- eq$Adj.Close[1:nrow(eq)-1]
nt1 <- eq$Adj.Close[2:nrow(eq)]
eq$return <- percent(c(NA, nt1/nt-1), accuracy = 0.0001)
return(eq)
}
Where it takes a dataframe as a parameter and returns the dataframe transformed in a way that I intend to use.
This works fine when I use it on a single dataframe, after I assign the value of the function to it, which is why I tried to assign it to get(x).
Next I thought of modifying my function, so that I just had to call it in sapply and not have to assign it to anything, using the assign() function, so it would change the variable in the global environment directly. Something like this:
sapply( dfs, function(x) {formatear(get(x))})
By changing formatear() to:
formatear <- function(eq){
eq$Volume <- NULL
eq$Date <- as.Date(eq$Date)
nt <- eq$Adj.Close[1:nrow(eq)-1]
nt1 <- eq$Adj.Close[2:nrow(eq)]
eq$return <- percent(c(NA, nt1/nt-1), accuracy = 0.0001)
assign(deparse(substitute(eq)), eq, envir = globalenv()) #Changed here
}
I used deparse(substitute(eq)) because assign() only takes string to find the variable in the global environment. I found that piece of code online too. This didn't work, it created new dataframes (with the correct format tho) with weird names:
structure(list(Date = structure(c(16962, 16965, 16966, 16967, #this is one of the names of these new dataframes
It also returns:
In assign(deparse(substitute(eq)), eq, envir = globalenv()) : only the first element is used as variable name
And it didn't even loop through all of the dataframes, just two. So that's the whole story, I don't know what else to do and googling doesn't seem to help. Advice at any problem I explained would be welcomed. Also, maybe it would interesting to know if there's a way to store all the dataframes in my environment into a vector so I can use a for loop? Anyway, thank you in advance.