How to subtract specific values from one column based on values in another column?

I have 188 rows and +1000 columns with variables. 3 of these columns are stating 3 different diagnosis with a logical operator, so 1 for every time the person has the disease of that column. In other columns I have for example age. So it looks like this:

Observations Diagnosis 1 Diagnosis 2 Diagnosis 3 Age
1 0 0 1 45
2 0 1 0 95
...
188 0 1 0 67

I wold like to get the age of only the people with each of the individual diseases. So I need to somehow specify the row of then Diagnosis = 1 and then subtract the column Age, but only for those specific rows.

Can anyone help with this?

As a beginner to R you may benefit from studying this useful book.
https://r4ds.had.co.nz/
Particularly chapter 5

It will show you how to use the most useful tools to select rows and columns from your data.

Hi Nirgrahamuk,

Thank you for the suggestion.
Do you maybe a suggestion for the specific problem?

Best, Charlotte

first use dplyr::filter() for the disease(s) of interest (row selection) , then use dplyr::select() for age (column selection)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.