0

I have a vector with mortality rates and I am trying to convert rates to probabilities.

I use this code:

v_p_HDage <- rate_to_prob(v_r_HDage)

But I get an error: Error in -r : invalid argument to unary operator

Also tried v_p_HDage <- (1-exp(-v_r_HDage))

But I get the same error.

My data v_p_HDage as it appears in R

Any ideas? Something wrong with the data?

aaPe
  • 3
  • 3
  • Welcome to Stack Overflow! It is helpful if you can post a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), and not post data as images. It looks from your screenshot that `v_r_HDage` is a character so you won't be able to apply the `-` operator to it. You probably want to convert it with `as.numeric()`, although I can't tell from the screenshot if it's a dataframe or nested list, so it's impossible to say what needs to go in the `as.numeric()` parentheses. – SamR Oct 03 '22 at 09:50
  • Original data looks like this Year;Age;Female;Male;Total 2021;0;0,00165;0,00203;0,002069453 2021;1;0,0002;0,0002;0,000193653 2021;2;0,0001;0,00013;4,93E-05 2021;3;0,00003;0,0001;0,000113588 2021;4;0,00007;0,00014;0,000113636 2021;5;0,00003;0,00006;9,71E-05 2021;6;0,00007;0,00005;4,86E-05 `Mortality_2022 <- read.csv("C:\\mortality.csv") v_mort <- Mortality_2022 %>% select(Total) %>% as.matrix() names(v_mort) <- Mortality_2022$Age v_r_HDage <- v_mort[(n_age_init + 1) + 0:(n_cycles - 1)] v_p_HDage <- rate_to_prob(v_r_HDage)` – aaPe Oct 03 '22 at 10:39
  • Sry missed some of the code: `cycle_length <- 1 n_age_init <-65 n_age_max <- 100 n_cycles <- (n_age_max - n_age_init)/cycle_length` – aaPe Oct 03 '22 at 11:09
  • I think thats it. Any ideas? – aaPe Oct 03 '22 at 11:11
  • It's still hard to reproduce this. I can't tell what types your columns have. If you edit your question to post a sample of your data with `dput(head(Mortality_2022))` it will be easier for someone else to reproduce this. – SamR Oct 03 '22 at 11:26
  • Thanks! Repostet as a new thread to tidy up a bit! https://stackoverflow.com/questions/73935217/rate-as-probability-in-r-error-in-r-invalid-argument-to-unary-operator – aaPe Oct 03 '22 at 12:11
  • OK I am going to vote to close this question then. – SamR Oct 03 '22 at 12:24

0 Answers0