Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through data frames based upon name

Tags:

loops

r

I have another simple r question that hopefully someone can help with. I have a series of dataframes that have a repetitive name structure. I would like to loop through them and perform some analysis. Here is hardcoded example of what I want to do using some fake data:

#Create some fake data
n1 = c(2, 3, 5, 7) 
s1 = c(1, 1, 2, 0) 
b1 = c(6, 0, 0, 0) 
Tank001.df = data.frame(n1, s1, b1)

n2 = c(1, 2, 4, 6) 
s2 = c(2, 2, 0, 0) 
b2 = c(8, 9, 10, 0) 
Tank002.df = data.frame(n2, s2, b2)

n3 = c(7, 12, 0, 0) 
s3 = c(5, 3, 0, 0) 
b3 = c(8, 9, 10, 4) 
Tank003.df = data.frame(n3, s3, b3)

The first action I would like to automate is the conversion of 0 values to "NA". Here is the harcoded version but I would ideally automate this dependant on how many Tankxxx.df dataframes I have:

#Convert zeros to NA
Tank001.df[Tank001.df==0] <- NA
Tank002.df[Tank002.df==0] <- NA
Tank003.df[Tank003.df==0] <- NA

Finally I would like to complete a series of queries of the data, a simple example of which might be the number of values smaller than 5 in each dataframe:

#Return the number of values smaller than 5
Tank001.less.than.5 <- numeric(length(Tank001.df))
for (i in 1:(length(Tank001.df))) {Tank001.less.than.5[i] <- sum(Tank001.df[[i]] < 5,na.rm=TRUE)} 
Tank002.less.than.5 <- numeric(length(Tank002.df))
for (i in 1:(length(Tank002.df))) {Tank002.less.than.5[i] <- sum(Tank002.df[[i]] < 5,na.rm=TRUE)} 
Tank003.less.than.5 <- numeric(length(Tank003.df))
for (i in 1:(length(Tank003.df))) {Tank003.less.than.5[i] <- sum(Tank003.df[[i]] < 5,na.rm=TRUE)} 

Ideally I would also like to know how to write the results of such simple calculations to a new dataframe. In this case for example Less.than.5$TankXXX etc.

Any help would be greatly appreciated.

like image 804
user1912925 Avatar asked Mar 23 '23 07:03

user1912925


1 Answers

Create a list of your data.frames and use a combination of lapply and sapply as follows:

TankList <- list(Tank001.df, Tank002.df, Tank003.df)
lapply(TankList, function(x) {
  x[x == 0] <- NA
  sapply(x, function(y) sum(y < 5, na.rm = TRUE))
})
# [[1]]
# n1 s1 b1 
#  2  3  0 
# 
# [[2]]
# n2 s2 b2 
#  3  2  0 
# 
# [[3]]
# n3 s3 b3 
#  0  1  1 
like image 75
A5C1D2H2I1M1N2O1R2T1 Avatar answered Apr 01 '23 17:04

A5C1D2H2I1M1N2O1R2T1