# R array loops

```"E. Anthon Eff" <eaeff@mtsu.edu>
Date:   Tue, February 8, 2011 2:34 pm
To:   drwhite@uci.edu
```

## 0

Subject: Re: NA vs NaN From: "E. Anthon Eff" <eaeff@mtsu.edu> Date: Tue, February 8, 2011 3:57 pm To: drwhite@uci.edu Options: View Full Header | View Printable Version | Download this as a file | View Message Details | View as HTML

Doug: found this at http://cran.r-project.org/doc/manuals/R-intro.html

```       Note that there is a second kind of "missing" values which are
produced by numerical computation, the so-called Not a Number,
NaN, values. Examples are
```
```        > 0/0
```
```       or
```
```        > Inf - Inf
```
```       which both give NaN since the result cannot be defined sensibly.
```
```       In summary, is.na(xx) is TRUE both for NA and NaN values. To
differentiate these, is.nan(xx) is only TRUE for NaNs. Missing
values are sometimes printed as <NA> when character vectors are
printed without quotes.
```

So it seems that there is no need to convert NaN to NA (I never have). But, if you want to do it, something like this should work:

for (i in NCOL(sccs)) {

``` z<-which(is.nan(sccs[,i]))
is.na(sccs[z,i])<-TRUE
}
```

## 1

```A loop like this should work (but didn't get a chance to test it):

for (i in c("v108","v110","v112","v146")) {
z<-which(sccs[,i]==0)
is.na(sccs[z,i])<-TRUE
}
```

## 2

```On 2/8/2011 3:38 PM, drwhite@uci.edu wrote:
> Super, got it. Many thanks!
>> Doug, if the DoL variable is "q":
>>
>> z<-which(q==0)
>> is.na(q[z])<-TRUE
>>
>> Now the "0" values have been replaced by NA
> AND IF I Wanted to do this in a list?
>
>   q1<-sccs\$v108
>   z<-which(q1==0)
>   is.na(q1[z])<-TRUE
>
>   q2<-sccs\$v110
>   z<-which(q2==0)
>   is.na(q2[z])<-TRUE
>
>   q3<-sccs\$v112
>   z<-which(q3==0)
>   is.na(q3[z])<-TRUE
>
>   q4<-sccs\$v111
>   z<-which(q4==0)
>   is.na(q4[z])<-TRUE
>
>   q5<-sccs\$v146
>   z<-which(q5==0)
>   is.na(q5[z])<-TRUE
>
>   depvar<- q0
>   my_sccs<-data.frame(
>   dep_var=q0,
>   #prepsoil=sccs\$v109,
>   clearland=q1,
>   plantcrop=q2,
>   #sccs\$v110,
>   harvest=q3,
>   #sccs\$v112,
>   tendcrop=q4,
>   #sccs\$v111,
>   carry=q5,
>   #sccs\$v146,
>   #fetchwater=sccs\$v125,
>   #prepveg=sccs\$v116,
>   #cook=sccs\$v121,
>   plow=(sccs\$v243>1)*1,
>   extwar=sccs\$v892   #no comma
>   )
```