user6016731 user6016731 - 4 months ago 13
R Question

Adding new column to multiple data frames at the same time

I have 7 datasets DF1..DF7 & al of them have the data in the following format:

Lowerbound Upperbound
738.5349709 2418.259919
734.5215381 2160.478003
1826.381884 2753.12212
452.3846655 3013.798816
542.6524558 863.0564862
399.4240401 2862.168099


I want to calculate the difference between the 2 columns & add a 3rd column to all the data sets at the same time & call it "interval" .I used the following code:

filenames <- names(which(sapply(.GlobalEnv, is.data.frame)))
for(i in seq_along(filenames)) {
x <- get(filenames[i])
for(j in 1:length(x))
{
x$Interval[[j]] <- ((x$upper_bound[[j]] -x$lower_bound[[j]])/x$actual[[j]])/2
}
assign(filenames[i], x)
}


The column "interval" gets generated but the values populated are wrong :numeric(0) is the result that I get to see in the new column.

Can someone suggest an improvement? Thanks in advance!!

Answer

We get all the datasets in a list with mget, use lapply to loop over the list elements, create a new column 'Interval' using transform by getting the difference between 'Upperbound', 'Lowerbound' and dividing by 'actual' (not seen in your data, but I guess it is already there)

lapply(mget(paste0("DF", 1:7)), transform, 
                Interval = ((Upperbound - Lowerbound)/actual)/2)
Comments