本文介绍了通过变量为列表中的多个数据帧制作一个NA计数的数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含4个数据帧的列表:

I have a list containing 4 data frames:

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

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

所有数据帧都具有相同的变量名.我想创建一个新的数据帧,该数据帧按变量和数据帧计数NA的数量.结果输出应如下所示:

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)

到目前为止,我一直在使用lapply(listofdf, function(x) table(is.na(x[, 15])))检查列表中每个数据帧的NA,这很麻烦!

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 !

推荐答案

在所示示例中,NAs是字符串.

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

对于真实的NAs

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

这篇关于通过变量为列表中的多个数据帧制作一个NA计数的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 14:11