[R] nrow()

Erik Iverson eriki at ccbr.umn.edu
Tue Feb 22 17:41:58 CET 2011



Sandra Stankowski wrote:
> is.na function does'nt seem to work, but maybe I'm just dealing with it 
> in a wrong way.
> 
> here's an example
> 
>  > m <- c(2, 3, 5, 6, 3, 7, -99, -99, 6)
>  > n <- c(1,1,1,1,1,2,2,2,2)
> 
> so my matrix contains certain missing values

Thank you for the example.
You're constructing a data.frame, not a matrix.
Those are two separate classes in R.

>  > m[m==-99] <- NA
>  > o <- data.frame(m, n)
>  > o
>    m n
> 1  2 1
> 2  3 1
> 3  5 1
> 4  6 1
> 5  3 1
> 6  7 2
> 7 NA 2
> 8 NA 2
> 9  6 2
> 
> "2" stands for february
> 
>  > february <- which(o[,2]==2, arr.ind = TRUE)
>  > prec_feb <- sum(o[february,1], na.rm = TRUE)
>  > prec_feb
> [1] 13
> 
> And now I need to know the exact number of rows, where "m"  contains a 
> value. to know how many days a month give any information. (to create 
> monthly means and stuff)

You might find ?complete.cases useful.

Also try:

sum(!is.na(o$m))

?tapply may also be useful in general: e.g.,

tapply(o$m, o$n, mean, na.rm  = TRUE)

None of this is tested...

> 
> hope this explains, what I need to know.
> 
> Thanks,
> S.
> 
> 
> 
> 
> 
> Am 22.02.2011 16:50, schrieb Erik Iverson:
>> Sandra,
>>
>> Please provide a small, reproducible example of this issue.
>> You probably want to use ?is.nan and not the inequality
>> operator.
>>
>> Similar example, contrast:
>>
>> x <- NA
>> is.na(x)
>> x == NA
>>
>> Sandra Stankowski wrote:
>>> Hey there,
>>>
>>> I tried to count the number of rows, where my data isn't NaN in a 
>>> certain column.
>>>
>>> this was my guess:
>>>
>>> (given is a data frame with 2069 rows and 17 cols)
>>>
>>> NROW(data[jan,16] != NaN)
>>>
>>> ("jan" is defined this way: jan <- which(data[,2]==1, arr.ind= TRUE))
>>>
>>>
>>> but I only get the number of columns where my data is "1" in the 
>>> second col. R isn't removing the NaN.
>>> na.rm isn't working here.
>>>
>>> I would appreciate your help.
>>>
>>> ______________________________________________
>>> R-help at r-project.org 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.
>>
>



More information about the R-help mailing list