Wednesday 15 April 2015

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



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

i have list containing 4 info frames:

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

all info frames have same variable names . want create new info frame counts number of nas variable , info frame. resulting output should 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 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]))) check nas of each info frame in list , cumbersome !

in illustration showed, nas 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))))

r list data.frame na

No comments:

Post a Comment