I have 342 continuous variables for proteins for which I would like to create a binary variable for a predetermined cut-off. E.g., if the continuous variable "HsCD00076570" has a value > 2, then its analogous binary variable, HsCD00076570_b, will have a value of 1 (else 0). While I know how to create such variables using dplyr::mutate(), I would like to automate the process so that I do not have to manually create 342 new variables. That's where I'm stuck!
Here is an example of what two of the continuous variables look like.
HsCD00076570 | HsCD00300736 |
---|---|
1 0.896 | 1.01 |
2 0.843 | 0.902 |
3 0.832 | 0.892 |
4 0.962 | 0.884 |
5 0.911 | 0.968 |
I have tried using lapply like so:
data6 <-lapply(data5[38:ncol(data5)], function(protb, protname) { #Apply loop to relevant columns in data
data5 %>% mutate(protb = if_else(protname > 2, #Create binary variable at cut-off of 2
1,
0)
)
}
)
Each observation has a unique ID variable, if that helps.
Thank you in advance!
Also, sorry I don't know how to use "dput" yet to include the header of my data in this post.