Making a data frame of count of NA by variable for multiple data frames in a list

杀马特。学长 韩版系。学妹 提交于 2019-12-08 09:23:12

问题


I have a list containing 4 data frames:

> names(listofdf) [1] "q12014local" "q12014national" "q22014local" "q22014national"

All the data frames have the same variable names . I want to make a new data frame which counts the number of NAs by variable and by data frame. The resulting output should look like this:

               v1    v2    v3    v4    v5    v6    v7
q12014local    328   278   1786  0     0     12    1
q12014national  0    100   124   0     0     7     0
q22014local     0    0     0     0     0     289   0
q22014national  423  0     10    10    78    0     0    

Here's a reproducible example:

> df1 <- data.frame(v1 = c(1:5), v2 = c("apple", "pear", NA, "peaches", NA), v3 = c("sunday", "monday", NA, NA, NA))

> df2 <- data.frame(v1 = c(2, 7, NA, NA, "9"), v2 = c("plum", NA, "kiwi", NA, "jackfruit"), v3 = c(NA, NA, "saturday", NA, "wednesday"))

> df3 <- data.frame(v1 = c(12, NA, NA, NA, 8), v2 = c("pineapple", "guava", "lytchee", NA, NA), v3 = c("tuesday", "thursday", "friday", NA, "monday"))

> listofdf <- list(df1, df2, df3)

So far I've been using lapply(listofdf, function(x) table(is.na(x[, 15]))) to check the NAs of each data frame in the list and this is cumbersome !


回答1:


In the example showed, NAs are strings.

 names(listofdf) <- c("q12014local" , "q12014national", "q22014local")
 as.data.frame(t(sapply(listofdf, function(x) colSums(x=='NA'))))
 #                v1 v2 v3
 #q12014local     0  2  3
 #q12014national  2  2  3
 #q22014local     3  2  1

For real NAs

 t(sapply(listofdf, function(x) colSums(is.na(x))))


来源:https://stackoverflow.com/questions/26743822/making-a-data-frame-of-count-of-na-by-variable-for-multiple-data-frames-in-a-lis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!