0

I am creating multiple data frames inside a loop using assign, but once they have been created, how do I modify them inside the same loop?

For example the following code...

for(i in 1:3) {
assign(paste0("df", i),data.frame(A=c('a','b','c'),B=c("","","")))
}

create three dataframe, namely df1,df2, and df3 each looking like this...

  A B
1 a  
2 b  
3 c  

With the B column blank.

Desired output is to have the value of 'i' inside the B column while creating df[i]. So df2 would be...

  A B
1 a 2
2 b 2
3 c 2

Important: Note that while I could do that inside the assign command itself in this case, in the larger problem that I am working on, I need to do the assignment outside the assign command as the dataframe I am creating is actually a subset of a larger dataframe not a new dataframe itself.

I have tried...

for(i in 1:3) {
assign(paste0("df", i),data.frame(A=c('a','b','c'),B=c("","","")))
paste0("df", i)$B <- i
}

... which doesn't work. What can work in place of paste0("df", i)$B <- i?

9
  • 4
    why clutter your global environment? Why not have all your dataframes in a list? And if the dataframe is a subset of the larger dataframe, why not use any of the packages often used for grouped operations? Commented Dec 14, 2015 at 11:03
  • @Heroka, would having the dataframe in a list make this dynamic modification easier? Commented Dec 14, 2015 at 11:05
  • @Heroka, I have to use dynamic subsetting, where even the number of subsets vary based on input. This is the easiet way I could figure out. Commented Dec 14, 2015 at 11:06
  • can you elaborate a bit on the larger problem you're trying to solve? How are subsets determined/made? Do they overlap? Commented Dec 14, 2015 at 11:07
  • @Heroka... I have a larger dataframe to be split into multiple subsets based where my input is the 'number of subsets' and '% of observations'. So I can have a single subset containing 100% values of larger set of 3 subsets containing 50%, 30% and 20% values respectively. And I have to give labels to each subset which specify the percentage. Commented Dec 14, 2015 at 11:11

2 Answers 2

3

I always try to keep things I will doing the same things to inside something. that can be a dataframe or a list, but it won't clutter my global environment and object manipulation is easier.

###create some data

set.seed(123)
nobs=100
dat <- data.frame(id=1:nobs,x=rnorm(nobs),y=runif(nobs))

n_subsets = 3
percentages = c(50,30,20)

#create sample_flags: list of 'labels', with
#each label being x percent of total

subset_labels <- sprintf("%.f%%",percentages)
subset_flags <- sample(rep(subset_labels , times=percentages*nrow(dat)/100))

#or, depending on larger problem
#subset_flags <- sample(subset_labels, size=nrow(dat), prob=percentages/100, replace=T)

#(might not work nicely with all numbers of obs, but I'm guessing you've solved that)

#random part
dat$mysubset <- subset_flags

#do stuff for each subset, like mean of y orcount
library(data.table)

setDT(dat)[,.(.N, meany=mean(y)),mysubset]

> setDT(dat)[,.(.N, meany=mean(y)),mysubset]
   mysubset  N     meany
1:      30% 30 0.5632690
2:      50% 50 0.4717880
3:      20% 20 0.405884

or if you really want a list

mylist  <- lapply(1:3,function(x){
  data.frame(A=c('a','b','c'),B=c("","",""))
}
)
mylist <- lapply(1:3, function(i){
  r <- mylist[[i]]
  r$i <- i
  r
})
Sign up to request clarification or add additional context in comments.

Comments

2

As comments say, this is a good opportunity to use a list (based on the limited information given).

I would:

l = lapply(1:3, function(i){
   data.frame(A=c("a", "b", "c"), B=i)
})

If you desperately want to assign column B separately:

l = lapply(1:3, function(i){
   x = data.frame(A=c("a", "b", "c"))
   x$B = i
   x
})

As a side note, I'm not sure what benefit you get from assign so I've not included it in my example.

Comments

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.