[R] For-loop dummy variables?
gravityflyer
gravityflyer at yahoo.com
Tue Oct 19 05:24:12 CEST 2010
Hi everyone,
I've got a dataset with 12,000 observations. One of the variables
(cleary$D1) is for an individual's country, coded 1 - 15. I'd like to create
a dummy variable for the Baltic states which are coded 4,6, and 7. In other
words, as a dummy variable Baltic states would be coded 1, else 0. I've
attempted the following for loop:
dummy <- matrix(NA, nrow=nrow(cleary), ncol=1)
for (i in 1:length(cleary$D1)){
if (cleary$D1 == 4){dummy[i] = 1}
else {dummy[i] = 0}
}
Unfortunately it generates the following error:
1: In if (cleary$D1 == 4) { ... :
the condition has length > 1 and only the first element will be used
Another options I've tried is the following:
binary <- vector(length=length(cleary$D1))
for (i in 1:length(cleary$D1)) {
if (cleary$D1 == 4 | cleary$D1 == 6 | cleary$D1 == 7 ) {binary[i] = 1}
else {binary[i] = 0}
}
Unfortunately it simply responds with "syntax error".
Any thoughts would be greatly appreciated!
--
View this message in context: http://r.789695.n4.nabble.com/For-loop-dummy-variables-tp3001396p3001396.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list