Homework help in R studio needed

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!

Hi KingKhan248, welcome to RStudio Community.

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).

In outline, the clearest way to do this is with the dplyr::mutate function using an case_when function to create the result column

suppressPackageStartupMessages(library(dplyr))

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))

df %>% mutate(outcome = case_when(
                        follow_up <  baseline ~ "negative",
                        follow_up == baseline ~ "unchanged",
                        follow_up >  baseline ~ "positive"
                        )
            )
#>   patient baseline follow_up  outcome
#> 1       1      100       105 positive
#> 2       2      110       100 negative
#> 3       3      109       102 negative
#> 4       4       99       105 positive
#> 5       5      103       100 negative
#> 6       6      101        97 negative
#> 7       7      125       108 negative
#> 8       8      130       120 negative

Created on 2020-07-25 by the reprex package (v0.3.0)

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.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

You don't need the print() statements. Try ifelse(df$difference > 0, "Recovered", "More treatment").

Hello and thank you for the reply.

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.

ifelse (df[1,4] > 0, print("Recovered"), print("More treatments") )
[1] "More treatments"
[1] "More treatments"

You could add it as a column to your data frame.

Thank you so much! Now how would I format this?

df$difference
[1] -5 10 7 -6 3 4 17 10
ifelse (df$difference > 0, "Recovered", "More treatments" )
[1] "More treatments" "Recovered" "Recovered" "More treatments"
[5] "Recovered" "Recovered" "Recovered" "Recovered"

This is what I'm currently getting, is there anyway to make it one line so that it is clear which difference is being referred to?