Problem: Write a code that changes the value of the 4th element of vector y to 6, then changes the value of last element to the same as 1st element and finally calculates the sum of all the elements.
Attempted solution:
y[4]<-6
y["last"]<- 1
sum(y)
Can't figure out what's wrong with this... help is much appreciated!
Your attempted solution is almost correct. The only problem is that the y["last"] line will not work because there is no element in the vector named last. You can fix this by using the length() function to get the length of the vector, and then using that value to index into the vector. For example:
y[length(y)] <- 1