Usage of R functions "table" & "ifelse" when NA’s exist

January 12, 2011

(This article was first published on Econometrics_Help, and kindly contributed to R-bloggers)

Most of the time I came across now and then in help posts questions regarding the mismatching total count of observations after employing the R functions “table” and “ifelse”. This usually creates frustration among fresh/part-time practitioners which ends up doubting the application and reverting back to their earlier tool.

However, this mismatching of total count happens only when you have NA’s in the data.

Thus, to always get the total count figures, we should make practice of use following options with respect to the R functions mentioned above:
table(varname1, varname2, useNA = c(“ifany”)) # in “table” usage of “useNA” option
ifelse( == T, ***, ifelse(varname1 > 100 & varname1 <= 110, 1, 0))
# in “ifelse” usage of “” option

***– here you need to provide which value to be taken if variable has values of NA.

Happy R Programming.

To leave a comment for the author, please follow the link and comment on their blog: Econometrics_Help. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)