[R] Number of NA's in every second column

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun May 20 21:53:16 CEST 2007


On Sun, 20 May 2007, Dimitris Rizopoulos wrote:

> for data.frames try:
>
> rowSums(sapply(dfr, is.na))
>
> whereas for matrices you could use:
>
> rowSums(is.na(mat))

That will also work for a data frame (is.na on a data frame gives a 
matrix).  However, I think the poster wants (for X a data frame)

rowSums(is.na(X[, seq(1, ncol(x), by=2)])

or (more likelu given the strict interpretation of his English expression 
and of the subject line)

colSums[is.na(X[, seq(1, ncol(x), by=2)])

which could be done a bit more efficiently as

sapply(X[, seq(1, ncol(x), by=2)], function(x) sum(is.na(x)))


> 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

That's not valid code: what is x?

>> 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
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list