My R code:
((x[1]-xm)^2)+((x[2]-xm)^2)+((x[3]-xm)^2)+((x[4]-xm)^2)+((x[5]-xm)^2)+((x[6]-xm)^2)
This computation would be much easier if i formulated the problem as a summation. How do I do that in r? Something like:
sum((x[i]-xm)^2) for i=1 to i=6?
x is a data frame.
x
is a data.frame it seems odd that this would work withx[1]
and notx[[1]]
. Are you surex
isn't a vector?sum((x-xm)^2)
works perfectly as Technophobe01 demonstrates.x
is a data frame, which makes your question less clear. That implies thatx[i]
is a column vector, so the question is what do you mean to sum column vectors? Do you want the overall sum? The row sums? Something else? A small reproducible example with sample data (preferably shared via simulation ordput()
) makes everything clear. See here for tips on asking good reproducible R questions.