Search code examples
rmultiplication

Multiplication a number nth times


New to R.

I have 2 columns 'X' and Y (x*0.78) repeated Y times as shown below

table of values

I am trying to multiply 'X' by 0.78 Y (nth)times e.g. x*0.78=ans*0.78=ans*0.78=ans*0.78.... however many times specified in row Y

So far have tried through writing own function but failed


Solution

  • Is this what you are after?

    # sample data
    my_data <- data.frame(x = c(4.98, 6.64, 1.66, 0, 3.32), y = c(18,5,8,10,8) )
    
    my_data$ans = my_data$x*0.78^my_data$y
    my_data$ans
    
    #[1] 0.05687641 1.91708378 0.22743899 0.00000000 0.45487797
    

    Or

    library(tidyverse)
    my_data <- tibble(x = c(4.98, 6.64, 1.66, 0, 3.32), y = c(18,5,8,10,8) )
    
    my_data %>% 
      mutate(ans = x * 0.78^y)