My dataframe is structured like the following:
ID A_L A_R B_L B_R
1 7 5 6 3
2 3 2 3 1
3 6 3 4 5
The goal is to create a new column for each existing column (besides the first column ID) dividing the value of the existing column through its L/R counterpart. So A_L_ratio = A_L/A_R and A_R_ratio = A_R/A_L etc.
I've tried to create a for-loop, using if/elseto differentiate between odd and even indices.
for (col in 2:length(df)) {
if( (col%%2) == 0){
a <- df[,col] / df[,col+1]}
else{
a <- df[,col] / df[,col-1]}
df[colnames(df[col])"_ratio"] <- a
}
But I seem to fail at R's syntax when it comes to naming the columns. Name should be the name of the column that is called in each loop df[,col] + the string _ratio. At the end I want to append that columne to df. Could someone tell me the right syntax to do this? Thanks a lot!