# Set up input values
A <- c(1,3,2,4,5,3,4,2,1)
# R doesn't like numbers as keys, so explicitly make them strings
B <- list("1"=2,"2"=1,"3"=4,"4"=2,"5"=3)

# Create a function to perform the calculation
CR_calc <- function(value, CR_value){
	# No casting needed as R only has one numeric type
	out <- (value/CR_value)^2
	# Handle special case with low value
	if(out < 1){
		out <- 0
	}
	return(out)
}

# Create vector for the result, intialised with NA values
result <- rep(NA,length(A))

# Explicit for loop
for(i in 1:length(A)){
	# Force the index for B to be character, otherwise it will be interpreted as an index
	result[i] <- CR_calc(A[i],B[[ as.character(A[i]) ]])
}


print(result)

# output:
# [1] 0.000000 0.000000 4.000000 4.000000 2.777778 0.000000 4.000000 4.000000
# [9] 0.000000
