I am trying to compute GPA from class grades(which have been normallized) I have for example the following matrix Master SID B2A B2B B2C C2A C2B C2C C118A C118B C118C 001 0.01 0.5 -0.4 1.2 -1.8 0.3 -0.3 0.4 0.5 002 0.01 0.5 -0.4 0.5 -0.4 1.2 -1.8 0.3 -0.3 003 0.04 0.05 0.5 -0.4 - 0.5 0.4 -1.2 1.8 0.3 etc Where each column has a zero mean and a standard deviation of 1. I want to calculate a weighted average for each row(student ID) that takes into account that B2A, C118A, C118B, and C118C are all 4 unit classes, and the rest, B2B, B2C, C2A,C2B,C2C are 5 unit classes I have tried Units<-c(4,5,5,5,5,5,4,4,4) Master$zGPA <-weighted.means(Master[,2:10],Units) But that gets me one number and not a vector. [[alternative HTML version deleted]]
On Jul 22, 2013, at 3:12 PM, Robert Lynch wrote:> I am trying to compute GPA from class grades(which have been normallized) > I have for example the following matrix > > Master > SID B2A B2B B2C C2A C2B C2C C118A C118B C118C > 001 0.01 0.5 -0.4 1.2 -1.8 0.3 -0.3 0.4 > 0.5 > 002 0.01 0.5 -0.4 0.5 -0.4 1.2 -1.8 0.3 > -0.3 > 003 0.04 0.05 0.5 -0.4 - 0.5 0.4 -1.2 1.8 > 0.3 > etc > > Where each column has a zero mean and a standard deviation of 1. I want to > calculate a weighted average for each row(student ID) that takes into > account that > B2A, C118A, C118B, and C118C are all 4 unit classes, and the rest, B2B, > B2C, C2A,C2B,C2C are 5 unit classes > > I have tried > Units<-c(4,5,5,5,5,5,4,4,4) > Master$zGPA <-weighted.means(Master[,2:10],Units) > > But that gets me one number and not a vector.Perhaps something along lines of Master$zGPA <-sapply( weighted.means(Master[,2:10], weighted.means, weghts=Units) (Untested in absence of data or name of package from which function is loaded.)> ?weighted.meansNo documentation for ?weighted.means? in specified packages and libraries: you could try ???weighted.means? --- David Winsemius Alameda, CA, USA
Hi, May be this helps: Master<-read.table(text=" SID??? B2A??? B2B??? B2C? C2A??? C2B??? C2C??? C118A??? C118B???? C118C 001??? 0.01??? 0.5????? -0.4??? 1.2????? -1.8??? 0.3????? -0.3????? 0.4?? 0.5 002??? 0.01??? 0.5????? -0.4??? 0.5????? -0.4??? 1.2????? -1.8????? 0.3? -0.3 003??? 0.04??? 0.05??? 0.5??? -0.4??? -0.5??? 0.4????? -1.2????? 1.8???? 0.3 ",sep="",header=TRUE) ?library(matrixStats) ? Master$zGPA<-rowWeightedMeans(as.matrix(Master[,-1]),Units) ?Master #? SID? B2A? B2B? B2C? C2A? C2B C2C C118A C118B C118C???????? zGPA #1?? 1 0.01 0.50 -0.4? 1.2 -1.8 0.3? -0.3?? 0.4?? 0.5? 0.035121951 #2?? 2 0.01 0.50 -0.4? 0.5 -0.4 1.2? -1.8?? 0.3? -0.3 -0.003902439 #3?? 3 0.04 0.05? 0.5 -0.4 -0.5 0.4? -1.2?? 1.8?? 0.3? 0.097804878 A.K. ----- Original Message ----- From: Robert Lynch <robert.b.lynch at gmail.com> To: r-help at r-project.org Cc: Sent: Monday, July 22, 2013 6:12 PM Subject: [R] weighted average I am trying to compute GPA from class grades(which have been normallized) I have for example the following matrix Master SID? ? B2A? ? B2B? ? B2C? C2A? ? C2B? ? C2C? ? C118A? ? C118B? ? C118C 001? ? 0.01? ? 0.5? ? ? -0.4? ? 1.2? ? ? -1.8? ? 0.3? ? ? -0.3? ? ? 0.4 ? ? ? ? ? 0.5 002? ? 0.01? ? 0.5? ? ? -0.4? ? 0.5? ? ? -0.4? ? 1.2? ? ? -1.8? ? ? 0.3 ? ? ? ? ? -0.3 003? ? 0.04? ? 0.05? ? 0.5? ? -0.4? ? - 0.5? ? 0.4? ? ? -1.2? ? ? 1.8 ? ? ? ? 0.3 etc Where each column has a zero mean and a standard deviation of 1.? I want to calculate a weighted average for each row(student ID) that takes into account that B2A, C118A, C118B, and C118C are all 4 unit classes, and the rest, B2B, B2C, C2A,C2B,C2C are 5 unit classes I have tried Units<-c(4,5,5,5,5,5,4,4,4) Master$zGPA <-weighted.means(Master[,2:10],Units) But that gets me one number and not a vector. ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
In that case: Master1<- Master Master1$zGPA<-sapply(seq_len(nrow(Master1[,-1])),function(i) weighted.mean(Master1[i,-1],Units)) Master1$zGPA #[1]? 0.035121951 -0.003902439? 0.097804878 all.equal(Master,Master1) #[1] TRUE A.K. ________________________________ From: Robert Lynch <robert.b.lynch at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Monday, July 22, 2013 6:35 PM Subject: Re: [R] weighted average weighted.mean is the function. ?My apologies for appending an s On Mon, Jul 22, 2013 at 3:31 PM, arun <smartpink111 at yahoo.com> wrote: Hi,> >I couldn't find the function `weighted.means` using ?weighted.means or ??weighted.means.? It would be useful to provide the library information. > > > > >----- Original Message ----- >From: arun <smartpink111 at yahoo.com> >To: Robert Lynch <robert.b.lynch at gmail.com> >Cc: R help <r-help at r-project.org> >Sent: Monday, July 22, 2013 6:26 PM >Subject: Re: [R] weighted average > >Hi, >May be this helps: >Master<-read.table(text=" >SID??? B2A??? B2B??? B2C? C2A??? C2B??? C2C??? C118A??? C118B???? C118C >001??? 0.01??? 0.5????? -0.4??? 1.2????? -1.8??? 0.3????? -0.3????? 0.4?? 0.5 >002??? 0.01??? 0.5????? -0.4??? 0.5????? -0.4??? 1.2????? -1.8????? 0.3? -0.3 >003??? 0.04??? 0.05??? 0.5??? -0.4??? -0.5??? 0.4????? -1.2????? 1.8???? 0.3 >",sep="",header=TRUE) >?library(matrixStats) > > >? Master$zGPA<-rowWeightedMeans(as.matrix(Master[,-1]),Units) >?Master >#? SID? B2A? B2B? B2C? C2A? C2B C2C C118A C118B C118C???????? zGPA >#1?? 1 0.01 0.50 -0.4? 1.2 -1.8 0.3? -0.3?? 0.4?? 0.5? 0.035121951 >#2?? 2 0.01 0.50 -0.4? 0.5 -0.4 1.2? -1.8?? 0.3? -0.3 -0.003902439 >#3?? 3 0.04 0.05? 0.5 -0.4 -0.5 0.4? -1.2?? 1.8?? 0.3? 0.097804878 >A.K. > >----- Original Message ----- >From: Robert Lynch <robert.b.lynch at gmail.com> >To: r-help at r-project.org >Cc: >Sent: Monday, July 22, 2013 6:12 PM >Subject: [R] weighted average > >I am trying to compute GPA from class grades(which have been normallized) >I have for example the following matrix > >Master >SID? ? B2A? ? B2B? ? B2C?? C2A? ?? C2B? ? C2C? ? C118A? ? C118B? ?? C118C >001? ? 0.01? ? 0.5? ? ? -0.4? ? 1.2? ? ?? -1.8? ?? 0.3? ? ? -0.3? ? ?? 0.4 >? ? ? ? ? 0.5 >002? ? 0.01? ? 0.5? ? ? -0.4? ? 0.5? ? ?? -0.4? ?? 1.2? ? ? -1.8? ? ?? 0.3 >? ? ? ? ? -0.3 >003? ? 0.04? ? 0.05? ?? 0.5? ? -0.4? ?? - 0.5? ?? 0.4? ? ? -1.2? ? ?? 1.8 >? ? ? ? 0.3 >etc > >Where each column has a zero mean and a standard deviation of 1.? I want to >calculate a weighted average for each row(student ID) that takes into >account that >B2A, C118A, C118B, and C118C are all 4 unit classes, and the rest, B2B, >B2C, C2A,C2B,C2C are 5 unit classes > >I have tried >Units<-c(4,5,5,5,5,5,4,4,4) >Master$zGPA <-weighted.means(Master[,2:10],Units) > >But that gets me one number and not a vector. > >??? [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. > >