Friday, 31 March 2017

How to access objects in R workspace by position?

I'm certain the answer is goingt to be very simple. Assume I have a number of data frames in my workspace, from a function that reads in a large number of files.



for(i in 1:10) {
df <- data.frame(matrix(1, nrow=10, ncol=20))
assign(paste0("df", i), df)
rm(list=c("df", "i"))
}


How can I access the data frame (or an object) by the position in the workspace as opposed to its name?




For example, assume I wanted to make a copy of the first object in my workspace. I can identify the object by ls()[1]. But, how could I extend that to actually access the data frame. Of course, temp <- ls()[1] doesn't work. Any help in pointing out the obvious would be helpful!

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...