[R-sig-teaching] Calculation with R
Malone, Christopher J
CMalone at winona.edu
Thu Jul 21 17:27:53 CEST 2016
Steve,
This should do the trick, subset rows == 1 in column 2 of matrix.
sum((data[data[,2]==1 ,1] - a)^2)
Have a good day,
Chris Malone
-----Original Message-----
From: R-sig-teaching [mailto:r-sig-teaching-bounces at r-project.org] On Behalf Of Steven Stoline
Sent: Thursday, July 21, 2016 10:20 AM
To: R-sig-teaching <R-sig-teaching at r-project.org>
Subject: [R-sig-teaching] Calculation with R
Dear All: good morning
I do need your help how to do the following calculation:
Assume we have a matrix 13x2 (say). Two columns named dataval and index
dataval index
20 1
11 0
34 0
54 1
76 1
76 0
61 1
88 1
91 0
11 0
23 1
45 1
53 0
data<-matrix(c(20,11,34,54,76,76,61,88,91,11,23,45,53,1,0,0,1,1,0,1,1,0,0,1,1,0),13,2)
data
I need to perform the calculation:
for a = 7 (say)
sum((data[ ,1] - a)^2)
only for index = data[ ,2] = 1
I am expecting the result to be equal to:
(20-7)^2 + (54-7)^2+(76-7)^2+(61-7)^2+(88-7)^2+(23-7)^2+(45-7)^2 = 18316
Any helps will be highly appreciated.
with many thanks
steve
-------------------------
Steven M. Stoline
1123 Forest Avenue
Portland, ME 04112
sstoline at gmail.com
[[alternative HTML version deleted]]
_______________________________________________
R-sig-teaching at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
More information about the R-sig-teaching
mailing list