Hi all, just switching to R from matlab and running into some begginner issues. I would appreciate your help:
I have a bunch of variables in my workspace that are not data frames. I want to convert them to data frames to use them in ggplot. I tried to do it with a for loop but couldn't get it. I understand the issue but not how to solve it. Here's the code that I tried
#Listing all the variables
variables=ls()
#Removing the one that I don't want to transform
variables=variables[-c(1)]
#For loop to transform every variable into data.frame
for (value in variables)
{
#Creating a new variable to keep "value" intact
name=value
name=data.frame(name)
}
Of course I am just creating a new dataframe that has one value, the variables name. Instead, I want to keep the content of the variable with the name of the variable but as a data frame
variables <- ls()
# removing the one that I don't want to transform
variables <- variables[-1]
#For loop to transform every variable into data.frame
for (value in variables){
text <- paste(value, "<- data.frame(", value, ")")
eval(parse(text=text))
}
However, I have to say that what you are doing is highly unidiomatic, so you'll probably have a better time with R if you learn a bit about how to do most common operations more "R-like".
One good book to learn it is https://r4ds.had.co.nz/. You can also take a look at "Advanced R" (https://adv-r.hadley.nz/). Don't be scared by "Advanced" in the name, just read first few chapters to get into the swing of things.
Thank you for your reply!! And also thanks for the resources. Will definetly take a look at them. I am aware that what I'm doing is also super inefficient so I'm working on improving my skills to get the most out of R. I recently read the paper on tidy data and I'm also practicing on datacamp.