[R] - Using a Loop To Make Code Compatible With R - | SheCodes

[R] - Using a Loop To Make Code Compatible With R

Learn how to use looping and code to check if a specific code appears in a R data frame. Set the variable for the ID to 0 or 1 if the code appears or not.

👩‍💻 Technical question

Asked 1 year ago in R by Mornie

I have a data frame with variables for participants ID, length of hospital stay, hospital stay and disease. There are multiple rows for each ID, as people have multiple values for the disease and length of hospital variable. I want a piece of code that goes through the disease variable, to see if code 111 appears for an ID, if this code appears then the hospital stay variable for that ID will be 0 but if any other disease code appears for the ID, the variable for the ID will be filled with 1. Could you write this code so it is compatible with R?

More coding questions about R

👩‍💻 Technical question

Asked 1 year ago in R by Mornie

I have a data frame with variables for participants ID, length of hospital stay, disease and whether they had stayed in hospital over the study period. There are multiple rows for each ID, as people have multiple values for the disease and length of hospital variable. I want a piece of code that goes through each group of IDs, notes if someone has been in hospital longer then 1 day and if they have I want to fill in the rest of the ID rows with 1. Could you write this code so it is compatible with R?