r - Counting 0`s, 1`s, 99`s and NA`s for each variable in a data frame -


i have data frame 118 variables 0's, 1's 99's , na's. need count each variable how many 99's, na's, 1's , 0's there (the 99 "not apply", 0 "no", 1 "yes" , na "no answer"). try table function works vectors, how can set of variables?

there little reproducible example of data frame:

forest<-c(1,1,1,1,0,0,0,1,1,1,0,na,0,na,0,99,99,1,0,na) water<-c(1,na,na,na,na,99,99,0,0,0,1,1,1,0,0,na,na,99,1,0) rain<-c(1,na,1,0,1,99,99,0,1,0,1,0,1,0,0,na,99,99,1,1) fire<-c(1,0,0,0,1,99,99,na,na,na,1,0,1,0,0,na,99,99,1,1)  df<-data.frame(forest,water,rain,fire) 

and need write in data frame result variable, this:

    forest    water    rain    fire 1    8         5        8       6 0    7         6        6       6 99   2         3        4       4 na   3         6        2       4 

can't find dupe, here's comment answer:

a data frame list of columns. lapply apply function every item in input (every column, in case of data frame) , return list each result:

lapply(df, table) # $forest #  #  0  1 99  #  7  8  2  #  # $water #  #  0  1 99  #  6  5  3  #  # $rain #  #  0  1 99  #  6  8  4  #  # $fire #  #  0  1 99  #  6  6  4  

sapply lapply, attempt simplify result instead of returning list. in both cases, can pass along additional arguments function being applied, usena = "always" table have na included in output:

sapply(df, table, usena = "always") #      forest water rain fire # 0         7     6    6    6 # 1         8     5    8    6 # 99        2     3    4    4 # <na>      3     6    2    4 

for lots more info, check out r grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. vs. aggregate


to compare other answers: apply similar lapply , sapply, intended use matrices or higher-dimensional arrays. time should use apply on data.frame when need apply function to each row. functions on data frame columns, prefer lapply or sapply. reason apply coerce data frame matrix first, can have unintended consequences if have columns of different classes.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -