-3

dataset being used is in this google sheets link https://docs.google.com/spreadsheets/d/1eV33Sgx_UVtk2vDtNBc4Yqs_kQoeffY0oj5gSCq9rCs/edit#gid=1959019385&vpid=A1

AMC.dataset$ExamMC.A<-surveySP15$Exams_A
AMC.dataset$ExamMC.A<-factor(NA, levels=c("TRUE", "FALSE"))
AMC.dataset$ExamMC.A[AMC.dataset$Exams_A=="1 time"|AMC.dataset$Exams_A=="2-4 times"|AMC.dataset$Exams_A==">4 times"]<-"TRUE"
AMC.dataset$ExamMC.A[AMC.dataset$Exams_A=="0 times"]<-"FALSE"
AMC.dataset$ExamMC.A=as.logical(AMC.dataset$ExamMC.A)

I use these 5 lines of code to re-code all 9 of the Exams_A through Exams_I variables into logical binary outcomes of "True" for those who have answered 1 or more times for any of these 9 variables. I would like to combine all of these variables into a new column in the dataset in which for each observation row, if there is even one case that is "true" for any of the 9 exams_A through I in that entire row, the new variable outcome will read as "true" meaning they have at least once committed any of te 9 types of Exam academic misconduct recorded in the dataset. if there are no true outcomes in the observation row i would like the new variable outcome to read as "false" meaning that they (the observation row) has never committed Exam academic misconduct

what I have for this new variable's code is

surveySP15$ExamMC = any(surveySP15$ExamMC.A, surveySP15$ExamMC.B, surveySP15$ExamMC.C, surveySP15$ExamMC.D, surveySP15$ExamMC.E, surveySP15$ExamMC.F, surveySP15$ExamMC.G, surveySP15$ExamMC.H, surveySP15$ExamMC.I)

however this set up seems to be overridden by the last variable case in the string (AMC.dataset$ExamMC.I)

  • 2
    Please show a reproducible example and expected ouput – akrun Dec 04 '15 at 05:52
  • the original 9 variable values initially had 4 different levels of values "0 times", "1 time", "2-4 times", ">4times", i recoded these 9 variables to produce the output true if the value was 1 time or more, so "1 time", "2-4 times" and ">4times" all produced the logical outcome "TRUE" and the "0 times" value produced an outcome of "FALSE", – chris Hahn Dec 04 '15 at 06:03
  • i would like to create a new variable that combines all nine of these variable's outcomes so that if in one row, any of the 9 variables has a case that is true, the new variable will have an outcome of true; however if all 9 variables have the outcome of false, that is when i would like this new variable to have the outcome as False as well – chris Hahn Dec 04 '15 at 06:03
  • 1
    It would be more easier for others to understand and answer it if you show a small example and the expected output based on that. – akrun Dec 04 '15 at 06:05
  • If I could do that I wouldn't have had the problem, honestly thought I was giving you that sample you requested to begin with. It seems the person below you understood me though. Thank you. – chris Hahn Dec 04 '15 at 09:24
  • Yes, somebody took their time to provide a reproducible example. Anyway, my comment was to make it easier for others to understand your problem. You are the only person who knows your problem better than anybody else. – akrun Dec 04 '15 at 13:38
  • i hope this is better. it would have been easier to direct me to the page where it explains how to provide a reproducible sample. – chris Hahn Dec 04 '15 at 21:16
  • For future reference, [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) might be useful. – akrun Dec 05 '15 at 04:13

1 Answers1

1

Here is one way of doing it:

a <- c("0 times", "1 time", "2-4 times", ">4times")
b <- rev(c("0 times", "1 time", "2-4 times", ">4times"))
df <- data.frame(a, b)
df2 <- apply(df, 2, function(x) x %in% "0 times") ## you can also use x == "0 times". 
## %in% allows using vectors, i.e. x %in% c("0 times", "1 times")

apply(df2, 1, any)
# [1]  TRUE FALSE FALSE  TRUE

Expanding the last line to your data.frame:

dataset$new.variable <- apply(dataset[c("variable1.new", "variable2.new", 
"variable3.new", "variable4.new", "variable5.new", "variable6.new", 
"variable7.new", "variable8.new", "variable9.new")], 1, any)
Mikko
  • 7,530
  • 8
  • 55
  • 92