I have 90 variables:
x111 <- 23
x112 <- 54
...
x1130 <- 69
x2111 <- 149
x2112 <- 12
...
x2130 <- 45
x3111 <- 85
x3112 <- 105
...
x3130 <- 501
I need to calculate a sum of squares of each variable (SS), for example, x111^2 + x112^2 + ...+ x3130^2
The function I have is:
SSobs_calculator <- function(obs) {
obs_value <- (obs)^2
total_obs_value = total_obs_value + obs_value
return(obs_value)
}
The problem is I do not know how to pass variables. In the following code:
for(i in 1:90){
SSobs_calculator(paste0("x1_11",i)
}
would not work, because I can't pass variables that start with x1_2.. and x1_3... Also the index for each group goes to 30, and 90 will not be valid.
Should I join all variables into a data frame, and then square it? Is there any other solution?