Мы можем сделать это векторизованным способом в базе R:
set.seed(2017)
#Get columns to extract value from
df$y <- sample(ncol(df[-1]), nrow(df), replace = TRUE)
#Create a row/column matrix
mat <- cbind(1:nrow(df), df$y + 1)
#Get the values from particular position
vals <- df[mat]
#Change values at the position
df[mat] <- df$price
#Change values in price.
df$price <- vals
df
# ID price Alt1 Alt2 Alt3 y
#1 A 1 1.0 3 3.4 4
#2 A 4.5 2.0 5 3.0 1
#3 B 1 5.6 4 5.0 2
#4 B 7 3.0 6 3.2 4
data
df <- data.frame(ID = c('A', 'A', 'B', 'B'),
price = c(3.4, 4.5, 5.6, 3.2),
Alt1 = c(1, 2, 1, 3),
Alt2 = c(3, 5, 4, 6),
Alt3 = c(1, 3, 5, 7))