Loop through name-based data frames

I have one more simple question that hopefully someone can help. I have a series of data that have a repeating name structure. I would like to go through them and do some analysis. Here is the hard code 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 converting 0 values ​​to "NA". Here is the hacked version, but I would ideally automate it depending on how many Tankxxx.df sound frames 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 data queries, a simple example of which would be the number of values ​​less than 5 in each data frame:

 #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 data frame. In this case, for example, Less.than. 5 $ TankXXX, etc.

Any help would be greatly appreciated.

+4
source share
2 answers

Create a list your data.frame 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 
+4
source

This also works with one lapply and colSums :

 l <- list(Tank001.df, Tank002.df, Tank003.df) # create a list lapply(l, function(x) colSums("is.na<-"(x, !x) < 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 
+3
source

Source: https://habr.com/ru/post/1489415/


All Articles