0

I have successfully allocated dataframe names and populated them (see code) but I do not know how to subsequently reference them. So I loop through to assign df.test1 and populate it with some data 1 and so on. I know that the df has been created, and can view or summary it in the console, but not in the code.

I am pretty new to R so am not sure if some of the solutions I have looked at apply to me.

num.clusters <- 5
for (i in 1:num.clusters) {
  assign(paste("df.test",i,sep=""), paste("somedata", i))
}

This works but Then want to do something like:

View(df.test,i) 

to view whatever iteration from 1 to 5.

I want to be able to use the assigned dataframes like any other dataframe. I could hard code this as View(df.test1) but that would defeat the point. I also want to do other things with the datframe, e.g. subsetting.

I know this doesn't work. Would love to know what does.

Many thanks...

8
  • print(get(paste0("df.test",i)) gives the output of the iteration, If you just want to follow the step of the iteration, you can put this inside the loop cat("df.test",i,"\n") Commented Aug 10, 2019 at 13:49
  • Hi there, many thanks for getting back to me. I don't think I was clear. I will update my question. I want to be able to use df.test1 as a working datadrame, and df.test2 etc. Commented Aug 10, 2019 at 14:01
  • What do you mean by saying "working dataframe"? It is accessible already. If you want to get a dynamic access you can use get function as above. get(paste0("df.test",i)) returns what you want. I.e , say you assigned a df by assign(paste("df.test",i,sep=""), as.data.frame(1:10)) then you can subset it via get(paste0("df.test",i))[3,1] to reach the 3rd row of 1st column which is 3 for this example. Commented Aug 10, 2019 at 14:12
  • You are absolutely right! My bad. Great stuff maydin. Commented Aug 10, 2019 at 14:18
  • 1
    Possible duplicate of Dynamic data frame creation in R with custom names Commented Aug 11, 2019 at 15:32

1 Answer 1

1

Your question is the proof that the approach is problematic: avoid using assign in general because it makes accessing the variables afterwards awkward (among other issues).

A cleaner way is to just put your "data frames" (copying from your example) in a list:

num.clusters <- 5
df.test <- list()
for (i in 1:num.clusters) {
  df.test[[i]] <- paste("somedata", i)
}

Then you would just access them like this:

View(df.test[[i]])

If what you put in there was an actual data.frame (and not the strings you were using), you could then access its columns like any other data.frame:

df.test[[i]]$Name

Or

df.test[[i]][, "Name"]
Sign up to request clarification or add additional context in comments.

1 Comment

And I can still access the original dataframe column names? I realise my original example doesn't have a typical dataframe structure. But if it had columns like 'Name', 'DoB', 'Gender' etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.