[R] Assignment problems

Rui Barradas ruipbarradas at sapo.pt
Mon Apr 23 19:13:13 CEST 2012


Hello,

phillip03 wrote
> 
> Thank you Rui
> 
> Can you help me with my ifelse problem - I would like to add a list to my
> data.frame where avgflow in those rows where ONLY my country pair both are
> in euro
> 

First of all, try to use better (much, much better) code writing.
What are you trying to do here ???

trade<-data.frame(avgflow,EMU,stringsAsFactors=FALSE)

avgflowEURO <- rep(0, nrow(trade))

trade1 <- (for (i in 1:nrow(trade)){
			ifelse(EMU[i] == 1, avgflowEURO[i] <- avgflow[i], NA)
		})

And please note that I've already INDENTED it.
And separated it in LINES of code.
And used SPACES before/after '==', '<-', etc.
And why TWO assignments in the same instruction? (At first I couldn't even
see this.)
You'll need far more programming experience before using 2 or more
assignments.

Now for the question. You don't need a loop/ifelse. Nor to duplicate the
data.frame 'trade'.


avgflowEURO <- avgflow
avgflowEURO[ !EMU ] <- NA
trade <- data.frame(avgflow, EMU, avgflowEURO, stringsAsFactors=FALSE)

Rui Barradas



--
View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4581111.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list