Stat Programming #homework
Hey all,
need some help with this homework problem.
I need to find the difference between 2 values in the same row, but two different columns, and then print different messages based on whether the difference is positive, negative, or 0.
I started by first creating a data frame :
df <- data.frame(patient = c(1 , 2 , 3 , 4 , 5 , 6, 7 , 8),
baseline=c(100,110,109,99,103,101,125,130),
follow_up = c(105, 100, 102, 105, 100, 97, 108, 120))
Then I created another column : df$difference <- df$baseline - df$follow_up
Now what I want to do is write code that would cycle through each element in the column I created, and print the appropriate text based on value with respect to 0.
How do I do this? Any help is greatly appreciated!
Please read our Homework Policy and modify your post to comply with the guidelines.
As for your question, there is a hint provided in the third line. R has a function called ifelse(). Try using that. You can type ?ifelse in the console to check the function documentation (it also has examples).
I have modified the question and hopefully I am now following the guidelines specified in the Homework Policy.
I tried ifelse() but somehow my 8 element column came back with 10 different outputs.
ifelse((df$difference > 0), print("Recovered"), print("More treatment")
Also, the first entry is -5, which should return "More treatments", did so, but twice for some reason.
Try this also with nested ifelse logical tests for contrast. Take this not as an answer to the specific question but as a template for all problems involving creating a new variable based on a transformation of one or more other variables.
If there's a requirement to use base only just apply the same logic.