Hi R-users, How do I calculate a number of NA's in a row of every second column in my data frame? As a starting point: dfr <- data.frame(sapply(x, function(x) sample(0:x, 6, replace = TRUE))) dfr[dfr==0] <- NA So, I would like to count the number of NA in row one, two, three etc. of columns X1, X3, X5 etc. Thanks in advance Lauri [[alternative HTML version deleted]]
for data.frames try: rowSums(sapply(dfr, is.na)) whereas for matrices you could use: rowSums(is.na(mat)) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting Lauri Nikkinen <lauri.nikkinen at iki.fi>:> Hi R-users, > > How do I calculate a number of NA's in a row of every second column in my > data frame? > > As a starting point: > dfr <- data.frame(sapply(x, function(x) sample(0:x, 6, replace = TRUE))) > dfr[dfr==0] <- NA > > So, I would like to count the number of NA in row one, two, three etc. of > columns X1, X3, X5 etc. > > Thanks in advance > Lauri > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
sorry but I misread the part about every second column of the data.frame; in this case you could use: rowSums(sapply(dfr[seq(1, length(dfr), 2)], is.na)) I hope it helps. Best, Dimitris -- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting Lauri Nikkinen <lauri.nikkinen at iki.fi>:> Hi R-users, > > How do I calculate a number of NA's in a row of every second column in my > data frame? > > As a starting point: > dfr <- data.frame(sapply(x, function(x) sample(0:x, 6, replace = TRUE))) > dfr[dfr==0] <- NA > > So, I would like to count the number of NA in row one, two, three etc. of > columns X1, X3, X5 etc. > > Thanks in advance > Lauri > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm