Z score

6 messages
Open this post in threaded view
|

Z score

 Hi, I need to find the z-score of the data present in a speardsheet. The values needs to be calculated for each gene across the samples (refer the example). And, it should be a simple thing, but I am unable to do it right now ! The example re the structure of the spreadsheet is  - # Example: MyFile <- read.csv( text= "Names,'Sample_1','Sample_2','Sample_3' Gene_1,87,77,88 Gene_2,98,22,34 Gene_3,33,43,33 Gene_4,78,,81 ", header=TRUE, row.names=1, as.is=TRUE, quote="'", na.strings="" ) And, I think this formula that can be used for z score is - (x-mean(x))/sd(x) And, apply() function for rows should work. But bottomline - I am unable to do it correctly. Could you show me - using apply () or some other alternative function. Thank you. Cheers, Ved         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

Re: Z score

 Hello, Try the following. apply(MyFile, 1, scale) Hope this helps, Rui Barradas Em 24-10-2012 07:17, Vedant Sharma escreveu: > Hi, > > I need to find the z-score of the data present in a speardsheet. The values > needs to be calculated for each gene across the samples (refer the > example). And, it should be a simple thing, but I am unable to do it right > now ! > > The example re the structure of the spreadsheet is  - > > # Example: > > MyFile <- read.csv( text= > "Names,'Sample_1','Sample_2','Sample_3' > Gene_1,87,77,88 > Gene_2,98,22,34 > Gene_3,33,43,33 > Gene_4,78,,81 > ", header=TRUE, row.names=1, as.is=TRUE, quote="'", na.strings="" ) > > And, I think this formula that can be used for z score is - > > (x-mean(x))/sd(x) > > And, apply() function for rows should work. But bottomline - I am unable to > do it correctly. > > Could you show me - using apply () or some other alternative function. > > Thank you. > > Cheers, > Ved > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

Re: Z score

 In reply to this post by Vedant Sharma Hi, Try this:  res<-do.call(rbind,lapply(lapply(apply(MyFile,1,function(x) x[!is.na(x)]),function(x) (x-mean(x))/sd(x)),function(x) x[c("Sample_1","Sample_2","Sample_3")]))  res #         Sample_1   Sample_2   Sample_3 #Gene_1  0.4931970 -1.1507929  0.6575959 #Gene_2  1.1421818 -0.7179429 -0.4242390 #Gene_3 -0.5773503  1.1547005 -0.5773503 #Gene_4 -0.7071068         NA  0.7071068 A.K. ----- Original Message ----- From: Vedant Sharma <[hidden email]> To: R help <[hidden email]> Cc: Sent: Wednesday, October 24, 2012 2:17 AM Subject: [R] Z score Hi, I need to find the z-score of the data present in a speardsheet. The values needs to be calculated for each gene across the samples (refer the example). And, it should be a simple thing, but I am unable to do it right now ! The example re the structure of the spreadsheet is  - # Example: MyFile <- read.csv( text= "Names,'Sample_1','Sample_2','Sample_3' Gene_1,87,77,88 Gene_2,98,22,34 Gene_3,33,43,33 Gene_4,78,,81 ", header=TRUE, row.names=1, as.is=TRUE, quote="'", na.strings="" ) And, I think this formula that can be used for z score is - (x-mean(x))/sd(x) And, apply() function for rows should work. But bottomline - I am unable to do it correctly. Could you show me - using apply () or some other alternative function. Thank you. Cheers, Ved     [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

Re: Z score

 In reply to this post by Vedant Sharma Hi, In cases, with more sample columns, you could also use this:  res2<-t(sapply(lapply(apply(MyFile,1,function(x) x[!is.na(x)]),function(x) (x-mean(x))/sd(x)),function(x) x[colnames(MyFile)] )) res2  #        Sample_1   Sample_2   Sample_3 #Gene_1  0.4931970 -1.1507929  0.6575959 #Gene_2  1.1421818 -0.7179429 -0.4242390 #Gene_3 -0.5773503  1.1547005 -0.5773503 #Gene_4 -0.7071068         NA  0.7071068 A.K. ----- Original Message ----- From: Vedant Sharma <[hidden email]> To: R help <[hidden email]> Cc: Sent: Wednesday, October 24, 2012 2:17 AM Subject: [R] Z score Hi, I need to find the z-score of the data present in a speardsheet. The values needs to be calculated for each gene across the samples (refer the example). And, it should be a simple thing, but I am unable to do it right now ! The example re the structure of the spreadsheet is  - # Example: MyFile <- read.csv( text= "Names,'Sample_1','Sample_2','Sample_3' Gene_1,87,77,88 Gene_2,98,22,34 Gene_3,33,43,33 Gene_4,78,,81 ", header=TRUE, row.names=1, as.is=TRUE, quote="'", na.strings="" ) And, I think this formula that can be used for z score is - (x-mean(x))/sd(x) And, apply() function for rows should work. But bottomline - I am unable to do it correctly. Could you show me - using apply () or some other alternative function. Thank you. Cheers, Ved     [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.