Hi, Casey.
Two quick things:
1) All the variables have to be the same length. You can check this by doing
length(var)
for each of the variables numpeer, w9zd9_1, _2, and _3.
2) I'm assuming that w9zd9_1, _2, and _3 are numeric. You can check
to see if they're numeric using is.numeric(var), where var is the
variable that you're examining. If the variables are factors (but
should have been read in as numbers), you can convert them to numbers
using
new.var <- as.numeric(as.character(old.var))
for example. (The as.character() is just as important as the
as.numeric() step, so don't leave it out.)
Once you have numeric variables, your problem can be solved using:
# Initializing the empty variable
distot <- rep(NA, length(numpeers))
# To save typing later
idx1 <- numpeers == 1
idx2 <- numpeers == 2
idx3 <- numpeers == 3
# Creating distot
distot[idx1] <- w9zd9_1[idx1]
distot[idx2] <- ((w9zd9_1 + w9zd9_2) / 2)[idx2]
distot[idx3] <- ((w9zd9_1 + w9zd9_2 +w9zd9_3) / 3)[idx3]
Basically, the idea is that idx1, idx2, and idx3 are logical vectors
(TRUE / FALSE). When you use var[logical], it subsets out the values
of var which are TRUE. The key is that if you replace values in a
variable, the replacement set has to have the same number of values as
the set to be replaced -- hence, you have to use the same idx on both
the left-hand and right-hand sides.
Alternatively, you can do this using brute force and a for loop, but
it will take much longer than the above solution:
distot <- rep(NA, length(numpeer))
for (i in 1:length(numpeer)) {
if (numpeer[i] == 1) {
distot[i] <- w9zd9_1[i]
}
else if (numpeer[i] == 2) {
distotl[i] <- (w9zd9_1[i] + w9zd9_2[i]) / 2
}
else if (numpeer[i] == 3) {
distot[i] <- (w9zd9_1[i] + w9zd9_2[i] + w9zd9_3[i]) / 3
}
else {
distot[i] <- NA
}
}
Yours,
Olivia
On Mon, Sep 14, 2009 at 10:50 AM, Casey Klofstad <klofstad(a)gmail.com> wrote:
Hey all-
I'm sure this is easy, but I'm having a hard time figuring out how to
recode some data in R.
I have a variable "numpeers" which is valued 1, 2, or 3. I also have
three other variables called "w9zd9_1," "w9zd9_2," and
"w9zd9_3." I
want to use these variables to create a new item called "distot."
Specifically, here is what I want to do:
-if numpeers=1, then distot=w9zd9_1
-if numpeers=2, then distot=(w9zd9_1 + w9zd9_2)/2
-if numpeers=3, then distot=(w9zd9_1 + w9zd9_2 + w9zd9_3)/3
Thanks, in advance, for the help!
Best,
-c
--
Casey A. Klofstad
University of Miami
Department of Political Science
Coral Gables, FL
klofstad(a)gmail.com
http://www.as.miami.edu/personal/cklofstad/
-
Zelig Mailing List, served by Harvard-MIT Data Center
Send messages: zelig(a)lists.gking.harvard.edu
[un]subscribe Options:
http://lists.gking.harvard.edu/?info=zelig
Zelig program information:
http://gking.harvard.edu/zelig/
-
Zelig Mailing List, served by Harvard-MIT Data Center
Send messages: zelig(a)lists.gking.harvard.edu
[un]subscribe Options:
http://lists.gking.harvard.edu/?info=zelig
Zelig program information:
http://gking.harvard.edu/zelig/