NewbieStatGal
NewbieStatGal

Reputation: 11

Adding ALL True's into one column from various columns

I am new to Stackoverflow AND to Rstudio. I have a question that I think is pretty basic, but I am not sure how to do it.

I am currently doing some medical research right now. I will post a picture below so you can see what I want to do. Basically, I have been looking into specific surgeries that have been done “anteriorly.” All of these specific anterior surgeries I have in columns, such as “anterior procedure #1, anterior procedure #2, anterior procedure #3.” If they have had the surgery, it is put as “TRUE.” What I want to do, is I want to “add” the “trues” on all of these surgeries into one column/ group that will be named generally- Anterior Procedures.

HOWEVER, I have tried this before, and unfortunately, it would NOT count ALL of the trues. LIke in the 3rd row below, it has counted two trues as one. The column/group I aim to create with the additions together, will be used to run a risk ratio with another variable ( I.e. specific complication with surgery).

Thus, my question is, how do I make a new “column” or “group” with ALL of the additions of the “TRUEs” in the columns I have.

I am happy to clarify too + I thank you from the bottom of my heart.

enter image description here

Upvotes: 1

Views: 45

Answers (2)

Andy Eggers
Andy Eggers

Reputation: 612

Does this help?

> TRUE & TRUE
[1] TRUE
> TRUE + TRUE
[1] 2

Upvotes: 3

Martin Gal
Martin Gal

Reputation: 16978

Not exactly what you are asking for, but you could use

df$all_ant <- rowSums(df)

which gives you the number of TRUE values in your row. So in your case this returns

  procedure_1 procedure_2 all_ant
  <lgl>       <lgl>         <dbl>
1 TRUE        FALSE             1
2 FALSE       TRUE              1
3 TRUE        TRUE              2

Data

df <- readr::read_table2("procedure_1 procedure_2
                         TRUE FALSE
                         FALSE TRUE
                         TRUE TRUE")

Upvotes: 1

Related Questions