Home > Software design >  How do I find the proportion of a value in a variable on R?
How do I find the proportion of a value in a variable on R?

Time:06-27

I want to find how many people who were surveyed for HIV tested negative.

I have an objective titled as HIV. Where HIV=0 is people who are negative. HIV=1, 2, 3, 4 are different levels of viral load of all people who tested positive.

I want to know of all people who are under the HIV objective, how many have HIV = 0?

I especially want to know how to calculate this value when there are missing values. TIA.

CodePudding user response:

Assuming your data is a vector called HIV:

HIV <- rep(c(0, 1, 2, 3, 4, NA), each = 4)

Which looks like this:

 [1]  0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4 NA NA NA NA

You can use the following:

prop.table(table(Data)))

To get an table object like this:

HIV
  0   1   2   3   4 
0.2 0.2 0.2 0.2 0.2 
  • Related