I have a dataset that contains data about the diagnosis of a number of medical conditions in patients. The values in these dimensions/columns are NA (patient was not in the diagnosis process), 1 = yes (diagnosed with the condition), 2 = No (not diagnosed with the condition). I want to create a separate column that counts the number of columns having 1 for each patient ID. My data frame has 1000 separate dimensions and I need to specifically pick based on either the index of the column or column name. my dataset looks like this
ID A B C D
1 NA 1 2 1
2 1 1 2 1
3 NA 2 2 1
4 1 1 2 1
5 2 1 1 1
6 2 1 NA 1
I obviously have way more columns and rows than this and any solution will require to consider the indexing. The reason I need to do this is to know the number of medical condition a patient is suffering from. I was able to solve this problem in SPSS using the 'transform' option as SPSS is my application of choice for data analysis. I have been enjoying R and intrigued by the number of statistical learning algos available in it.
I have tried the apply
function and the colSums
but I am struggling with indexing as well as setting the count function = +1. I have looked into for loops as well with no luck into my specific problem.