I have a dataset with variables named as I10AA to I10ZZ and I11AA to I11ZZ. I want to create new variables IAA to IZZ, so that IAA = function(I10AA,I11AA).
As an highly simplified example.
set.seed(0)
df <- data.frame(I10AA=floor(runif(10,1,5)),I10AB=floor(runif(10,1,5)),
I11AA=floor(runif(10,1,5)),I11AB=floor(runif(10,1,5)))
fun <- function(x,y) (x+y)
results <- df %>% mutate(IAA = fun(I10AA,I11AA),IAB = fun(I10AB,I11AB))
print(results)
results is the final dataset I want.
Is there a way to do this with tidyverse?
In the original dataset, the variables are arranged as:
colnames(original_data) = "ID","I1AA", "I1AB", "I1AC", ... , "I1ZZ", "I2AA","I2AB",...,"I2ZZ",...,"I10AA",...,"I10ZZ","I11AA",..."I11ZZ"