If your data frame df
is exactly as you show, you can simply do
df[-ncol(df)] / df$len
If you have other columns to exclude, and you want them all included in the result, you can do something like
with(df, cbind(ID, df[!names(df) %in% c("ID", "len")]/len, len))
# ID A B C D E F len
# 1 1 0.4000000 0.800000 1.00 1.4000000 1.60 1.6000000 5
# 2 2 0.8333333 1.333333 0.50 0.1666667 0.00 0.6666667 6
# 3 3 0.6666667 0.750000 0.25 0.7500000 0.50 0.1666667 12
# 4 4 0.2000000 0.600000 0.20 0.6000000 0.70 0.8000000 10
# 5 5 0.0500000 0.100000 0.20 0.1000000 0.45 0.2500000 20
Also, as suggested by David in the comments, you can use data.table
library(data.table)
x <- c(1L, ncol(df))
setDT(df)[, names(df)[-x] := lapply(.SD, "/", df$len), .SDcols = -x]
which results in
# ID A B C D E F len
# 1: 1 0.4000000 0.800000 1.00 1.4000000 1.60 1.6000000 5
# 2: 2 0.8333333 1.333333 0.50 0.1666667 0.00 0.6666667 6
# 3: 3 0.6666667 0.750000 0.25 0.7500000 0.50 0.1666667 12
# 4: 4 0.2000000 0.600000 0.20 0.6000000 0.70 0.8000000 10
# 5: 5 0.0500000 0.100000 0.20 0.1000000 0.45 0.2500000 20
where df
is
df <- read.table(text = "ID A B C D E F len
1 2 4 5 7 8 8 5
2 5 8 3 1 0 4 6
3 8 9 3 9 6 2 12
4 2 6 2 6 7 8 10
5 1 2 4 2 9 5 20", header = TRUE)