Gurbe
Gurbe

Reputation: 371

How to apply the same code to a number of objects in R

I have some data frames which contain the same number of columns and named as mydf1 to mydf10 and want to aggregate, merge with a data frame that contain complete date, replace missing dates with 0, and merge with another column. The following code shows the steps I have taken to work on one data frame.

### Aggregate id and count occurrences
mydf0_1 <- aggregate(id ~ date,data = mydf0,FUN=length)

## rename second column
names(mydf0_1) [2] <- "count"

## Create a full data frame of date
nn <- data.frame(date = seq(as.Date("2000-01-01"), as.Date("2005-12-31"),
                            by="days"))

###  Merge the data with a full series
res <- merge(mydf0_1,nn,by.x='date',by.y='date',all.x=T,all.y=T)

## Replace NA with 0 count
res$count[is.na(res$count)] <- 0

## merge with another data frame 
st0 <- cbind(res, temp)

How could I automate the process and apply the same script to all data frames mydf0 to mydf10?

One of the data frames look as follows:

> dput(mydf0)
structure(list(id = c(285476L, 107062L, 138919L, 88350L, 71678L, 
226406L, 122913L, 36801L, 37201L, 83417L), date = structure(c(10957, 
10957, 10957, 10957, 10957, 10957, 10957, 10958, 10958, 10958
), class = "Date")), datalabel = "", time.stamp = "13 Apr 2015 15:20", .Names = c("id", 
"date"), formats = c("%9.0g", "%td"), types = c(253L, 255L), val.labels = c("", 
""), var.labels = c("", ""), expansion.fields = list(c("_dta", 
"_lang_c", "default"), c("_dta", "_lang_list", "default")), row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10"), version = 12L, class = "data.frame")

Upvotes: 0

Views: 68

Answers (1)

akrun
akrun

Reputation: 887941

We could place the data.frames in a list (mget(paste0('mydf', 0:10))) and apply the code.

  nn<- data.frame(date=seq(as.Date("2000-01-01"),
                       as.Date("2005-12-31"), by="days"))
  lst <- lapply(mget(paste0('mydf',0:10)), function(x) {
           d1 <- aggregate(cbind(count=id)~date, x, FUN=length)
           res <- merge(d1, nn, by='date', all=TRUE)
           res$count[is.na(res$count)] <- 0
           merge(res, temp, by='date', all=TRUE)
         })

NOTE: The temp object was not defined in the post.

Upvotes: 3

Related Questions