[R] Help with averaging

David Winsemius dwinsemius at comcast.net
Wed Jul 15 22:42:48 CEST 2009


You might want to look at the help page for:

?mean

and pay close attention to the na.rm= argument/


On Jul 15, 2009, at 12:51 PM, Amit Patel wrote:

>
> Hi
> I am using the following script to average a set of data 0f 62  
> columns into 31 colums. The data consists of values of ln(0.01) or  
> -4.60517 instead of NA's. These need to be averaged for each row  
> (i.e 2 values being averaged). What I would I need to change for me  
> to meet the conditions:
>
>
> 1. If each run of the sample has a value, the average is
> given
> 2. If only one run of the sample has a value, that value
> is given (i.e. no
> averaging)
> 3. If both runs have missing values, NA is given.
> I have tried changing (na.strings = "NA") to (na.strings =  
> "-4.60517") but this causes all the pairs with even one NA return an  
> NA value. I would prefer not to use for loops as this would slow the  
> script down considerably.
>
> #SCRIPT STARTS
> rm(list=ls())
> setwd("C:/Documents and Settings/Amit Patel")
>
> #na.strings makes na's readable
> zz <- read.csv("Pr9549_LabelFreeData_ByExperimentAK.csv",strip.white  
> = TRUE, na.strings = "NA")
>
> ix <- seq(from=2,to=62, by=2)
> averagedResults <- (zz[,ix] + zz[,ix+1])/2
> averagedResults <-  cbind(zz[,1],averagedResults )
> colnames(averagedResults)  <- c("PCI","G1-C1","G1-C2","G1-C3","G1- 
> C4","G1-C5","G1-C6","G1-C7","G1-C8",
> "G2-C9","G2-C10","G2-C11","G2-C12","G2-C13","G2-C14","G2-C15","G2- 
> C16",
> "G3-C17","G3-C18","G3-C19","G3-C20","G3-C21","G3-C22","G3-C23",
> "G4-C24","G4-C25","G4-C26","G4-C27","G4-C28","G4-C29","G4-C30","G4- 
> C31")
>
> write.csv(averagedResults, file = "Pr9549_averagedreplicates.csv",  
> row.names = FALSE)
> #SCRIPT ENDS
>
>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list