[R] Assignment problems
David Winsemius
dwinsemius at comcast.net
Mon Apr 23 23:37:05 CEST 2012
On Apr 23, 2012, at 5:21 PM, R. Michael Weylandt wrote:
> That's not the ifelse() that's the for loop returning NULL
> (everything's a function!). If you put the assignment inside you'll
> get expected behavior.
>
> x <- (for(i in 1:5) i) # Strange
> for(i in 1:5) x<- i # Normal (but notice you only get the last value
> because previous ones are overwritten)
>
Better would be to avoid the for-loop altogether. And phillip03 should
note: The for-loop does not create an environment where column names
are interpreted as object names and I'm guessing that 'avgflowEMU' is
not an object but rather a column name.
Depending on the unstated goal (and quite a few other unstated
concerns), something like this might make more sense:
EMU1993<-sum( data[ data$year==1993, "avgflowEMU" ], na.rm=TRUE)
phillip03: please stop using 'data' as an object name. It is also the
name of an R function.
--
David.
> Michael
>
> On Mon, Apr 23, 2012 at 4:26 PM, phillip03
> <phillipbrigham at hotmail.com> wrote:
>> Hi
>>
>>> EMU1993<-(for (i in 1:nrow(data)){
>> + ifelse(year==1992,sum(avgflowEMU),0)
>> + })
>>
>>> EMU1993
>> NULL
>>
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list