Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a new variable in R based on values from two other columns?

I have a dataset that looks like this:

Current Dataset

Where each patient has four rows, for both of their ears, at two timepoints. I want to create a new variable that takes from the first row of chemo dose 1, and the second row of chemo dose 2. My desired output is something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Desired Output

How can I create a variable like this in R?

>Solution :

Can you simply mutate(), using if_else()?

library(dplyr)

df %>% mutate(NEW_VARIABLE = if_else(Time_Point=="C1", Chemo_Dose1,Chemo_Dose2))

Output:

        Ear Study_ID Chemo_Dose1 Chemo_Dose2 Time_Point NEW_VARIABLE
1  Left Ear  CF41853        1200         300         C1         1200
2  Left Ear  CF41853        1200         300       Post          300
3 Right Ear  CF41854        1200         300         C1         1200
4 Right Ear  CF41854        1200         300       Post          300

Input:

structure(list(Ear = c("Left Ear", "Left Ear", "Right Ear", "Right Ear"
), Study_ID = c("CF41853", "CF41853", "CF41854", "CF41854"), 
    Chemo_Dose1 = c(1200, 1200, 1200, 1200), Chemo_Dose2 = c(300, 
    300, 300, 300), Time_Point = c("C1", "Post", "C1", "Post"
    )), class = "data.frame", row.names = c(NA, -4L))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading