0

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.

sam.cold
  • 17
  • 6
  • Please, provide a minimal reproducible example: [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – PaulS Jun 26 '22 at 11:05
  • `mean(HIV == 0, na.rm = TRUE)` for proportion and `sum(HIV == 0, na.rm = TRUE)` for count. – Ronak Shah Jun 26 '22 at 11:06

1 Answers1

2

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 
bpvalderrama
  • 357
  • 2
  • 8
  • does that automatically remove missing values? – sam.cold Jun 26 '22 at 11:09
  • I edited the post so you can see the outputs. Let me know if this answer doesn't solve your questions – bpvalderrama Jun 26 '22 at 11:13
  • so I think I was overcomplicating it and it does in fact consider those missing values. Thank you <333 – sam.cold Jun 26 '22 at 11:18
  • Nice! Since this answer your question, please consider to accept my answer (clicking the check icon). This would help others with the same problem to find a solution :D – bpvalderrama Jun 26 '22 at 11:20
  • @sam.cold Missings are removed, to include them in the output use `prop.table(table(Data), useNA='ifany'))`. Note that `prop.table` is the old name which is kept for back-compatibility, we should use `proportions` now. – jay.sf Jun 26 '22 at 12:52
  • I didn't know about the `proportions` function. Thank you! – bpvalderrama Jun 26 '22 at 13:25