# [R] matrix manipulation with a for loop

antonio rodriguez antonio.raju at gmail.com
Wed Nov 1 19:43:13 CET 2006

```Phil Spector escribió:
> Antonio -
>    You need the na.rm in the first invocation of sum -- is.na() will
> never
> return a missing value:
>
>   apply(F.zoo,2,function(x)sum(x >=5 & x <=
> 9,na.rm=TRUE)/sum(!is.na(x))*100)
>
>                                                           - Phil
Dear Phil,

I understand now. Many thanks!!

Best regards,

Antonio

>
>
> On Wed, 1 Nov 2006, antonio rodriguez wrote:
>
>> Phil Spector escribió:
>>> Antonio -
>>>    When you're operating on each column of a matrix, you really should
>>> consider the apply() function, which was written for the task.
>>> Also, it's usually easier to count things in R by taking the sum of
>>> a logical
>>> expression, rather than the length of a subsetted vector.
>>>    Here's code that will solve your problem:
>>>
>>>     apply(F.zoo,1,function(x)sum(x >=5 & x <= 9)/sum(!is.na(x))*100)
>> Dear Phil,
>>
>> The problem is that the columns have some missing values (NA's) so
>> the result for:
>>
>> apply(F.zoo,2,function(x)sum(x >=5 & x <= 9)/sum(!is.na(x))*100) #
>> yields:
>>
>> X.1   X.2   X.3   X.4   X.5   X.6   X.7   X.8   X.9  X.10  X.11
>> X.12  X.13
>>  NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
>> NA    NA
>> X.14  X.15  X.16  X.17  X.18  X.19  X.20  X.21  X.22  X.23  X.24
>> X.25  X.26
>>  NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
>> NA    NA
>>
>> So it is supposed that using na.rm=T should do the task, but if I write:
>>
>> apply(F.zoo,2,function(x)sum(F.zoo >=5 & F.zoo <=
>> 9)/sum(!is.na(F.zoo))*100,na.rm=T)
>>
>> I get:
>>
>> Erro en FUN(newX[, i], ...) : unused argument(s) (na.rm = TRUE)
>>
>> Antonio
>>
>>
>>
>>>
>>>                                        - Phil Spector
>>>                      Statistical Computing Facility
>>>                      Department of Statistics
>>>                      UC Berkeley
>>>                      spector at stat.berkeley.edu
>>>
>>>
>>> On Wed, 1 Nov 2006, antonio rodriguez wrote:
>>>
>>>> Hi,
>>>>
>>>> Having a matrix F.zoo (6575,189) with NA's in some columns I'm
>>>> trying to
>>>> extract from each column the percent of days within an specific range,
>>>> so I've wrote this procedure:
>>>>
>>>> length(subset(F.zoo[,86],(F.zoo[,86]>=5) & (F.zoo[,86]<=
>>>> 9)))/(length(F.zoo[,86])-length(subset(F.zoo[,86],is.na(F.zoo[,86]))))*100
>>>>
>>>> But to do this for each column (189) is pretty hard, so I want to
>>>> write
>>>> a function in order to perform this automatically, such I have the
>>>> percent value corresponding to a specific column. I' tried these two
>>>> formulas but I can't get it. I think the problem is how to set the
>>>> initial values for the loop:
>>>>
>>>> Formula1:
>>>>
>>>> nnn<-function(x){for (i in F.zoo[,i]){
>>>>    print(length(subset(F.zoo[,i],(F.zoo[,i]>=5) & (F.zoo[,i]<=
>>>> 9)))/(length(F.zoo[,i])-length(subset(F.zoo[,i],is.na(F.zoo[,i]))))*100)
>>>>
>>>> }
>>>> }
>>>>
>>>> Formula 2:
>>>>
>>>> H<-t(matrix(1,189))
>>>>
>>>> nnn<-function(x){for (i in col(H){
>>>>    print(length(subset(F.zoo[,i],(F.zoo[,i]>=5) & (F.zoo[,i]<=
>>>> 9)))/(length(F.zoo[,i])-length(subset(F.zoo[,i],is.na(F.zoo[,i]))))*100)
>>>>
>>>> }
>>>> }
>>>>
>>>> Thanks,
>>>>
>>>> Antonio
>>>>
>>>> ______________________________________________
>>>> R-help at stat.math.ethz.ch mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help