I'm working on a R-Script where I have to create multiple variables for multiple existing dataframes. For example: I got the three dataframes AAA, BBB and CCC, and each one of them has a column named "height". I would like to get this:
AAA_Skal <- mean(AAA$height) / 6
BBB_Skal <- mean(BBB$height) / 6
CCC_Skal <- mean(CCC$height) / 6
without actually writing a entire line of code for each dataframe. (The reason for this is that I got more than just 3 dataframes and I have to proceed a lot of code with them).
What I tried is the following:
dfs <- c("AAA", "BBB", "CCC")
Skal <- function(x) {
sprintf("%s_Skal", dfs[x]) <- mean(sprintf("%s_$height", dfs[x])) / 6
}
I should then be able to type Skal(1) to get
AAA_Skal <- mean(AAA$height) / 6
First I thought it isnt working because sprintf gives "AAA" as an output, with quotation marks. So I tried as.name(sprintf()), but it doesent work either. I hope someone can help me on this issue, and sorry for my bad english.