Perhaps someone could help me to elaborate better on the title of this post.
I am trying to apply the exact same formula to several sites (Australia, Mexico and France). Each site has the same variables (P, PET). To reproduce the example I have created the following data frame:
df <- data.frame(Australia.P = c(2015), Australia.PET = c(739),
Mexico.P = c(1284), Mexico.PET = c(808),
France.P = c(2010),France.PET = c(958))
for each site I would like to calculate the following formula: x = P * PET and it can be done as follows:
x_a <- df$Australia.P * df$Australia.PET # and so on for each Country
x_m <- df$Mexico.P * df$Mexico.PET
x_f <- df$France.P * df$France.PET
df_new <- data.frame(x_a, x_m, x_f)
and my questions is: How could I compute "x = P * PET" for all sites (without having to type each site variable) at once and end up with the same results in a data frame? This would help me in case I had a hundred sites (Countries)
I would appreciate any help. Thank you.
You could do a combination of
pivot_longer,separate_wider_delim,reframeandpivot_widerto get this:Output