1

2=female, 1=male. q1, q2, q3 are variables. I want to know what is the ratio between male and female. For example: the ratio of the first is 2/3=0.66. The same answer for the second. About the third, i need to divide in 2 because -99 is missing value and the answer should be 1/2=0.5. how can i do it?

q1      q2      q3
2.00    1.00    1.00
2.00    1.00    1.00
2.00    1.00    -99.00
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
E.eyal
  • 11
  • 1

2 Answers2

1

If df is your data.frame or matrix, you could calculate the sums of 1 appearances per row and divide by the sums of non -99 appearances

rowSums(df == 1)/rowSums(df != -99)
# [1] 0.6666667 0.6666667 0.5000000

You could wrap it up into round if you like.

It is also probably better to convert missing values to NAs using something like

is.na(df) <- df == -99

And then

rowSums(df == 1, na.rm = TRUE)/rowSums(!is.na(df))

As a side note, I don't know what is your data structure but in most cases it is better to work with matrices if you only have numerical values there.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
0

First, change the encoding of the NA values from -99to an actual NAvalue. That way you can easily exclude it from any method.

I would then also just change the value for female to 0. The you can just calculate your ratio by summing up the rows and dividing by the number of columns, excluding those with NA.

matt_jay
  • 1,241
  • 1
  • 15
  • 33