I am a novice of R. This variable, test.ind, has 2 levels. It's an indicator for whether the customer participated in the program; "R"=participated, "C"=nonparticipant (control).
The question is: Convert the variable test.ind to a 0/1 indicator variable (an integer), and name it treat, where customers enrolled in the program are assigned a value of 1, and customers not enrolled in the program are assigned a value of 0.
you gave us that C is 1 and R is 2, we merely subtracted 1 to make C 0 R 1
if C was 2 and R was 1 then to make C 0 R 1, you would
multiply by -1 for C -2 R -1
then add 2 so C 0 R 1
ok, so your dput() revealed to us the internal representation of the factor.
also can use as.integer() to see the factors internal integer representation.
Or can avoice considering that and use a logic test to directly assign the value you want conditionally.
Perhaps you prefer that approach. In this case I can say if the factor text is 'C' give me 0, otherwise give me 1. (otherwise includes 'R')
new_data <- structure(list(test.ind = structure(c(1L, 2L, 2L, 2L, 2L), .Label = c("C",
"R"), class = "factor")), row.names = c(NA, 5L), class = "data.frame")
new_data$test.ind
# > new_data$test.ind
# [1] C R R R R
# Levels: C R
as.integer(new_data$test.ind)
# > as.integer(new_data$test.ind)
# [1] 1 2 2 2 2
new_data$test.ind <- ifelse(new_data$test.ind=="C",0,1)
new_data$test.ind
# > new_data$test.ind
# [1] 0 1 1 1 1
yes, if you did as.integer(unique( newdata$test.ind)) you would get the unique integer values.
But this unique stuff isnt much use aside from understanding your data. its not required to change your data in a mechanical sense.
to change "C" into 0, and "R" into 1
do
Thanks. I checked the help ifelse file.
"ifelse(test, yes, no) means yes return values for true elements of test. no return values for false elements of test."
So can you check whether my understanding is correct or wrong? In your code, the test is the value = C. And if it's correct, we assign this variable 0. Otherwise, we assign 1.