[R] R Help-how to use sapply w/tapply

arun smartpink111 at yahoo.com
Tue Oct 15 00:08:03 CEST 2013


Gene[,-1] <- lapply(Gene[,-1],function(x) {x[sample(length(x),1)] <- NA;x})


I got it guys 

MeanofGenotype <-sapply(2:8, function(k) tapply(Gene[,k] , Gene$Genotype, mean)) 

I was wondering if now you guys can help me on how to insert a random NA to each column. 

The question is 

In the original data, for each numeric column, select randomly a
 number to set to NA. So, each column will have 11 numbers and one NA. 

On Monday, October 14, 2013 4:52 PM, arun <smartpink111 at yahoo.com> wrote:

(Please use ?dput() to share the example dataset. Avoid using images to show dataset. Also, please read the posting guide esp. regarding home work, assignments etc.)

res <- sapply(Gene[,-1],function(x) tapply(x,list(Gene$Genotype),mean))

res2 <-  aggregate(.~Genotype, data=Gene,mean)

 res3 <- ddply(Gene,.(Genotype),numcolwise(mean))

#[1] TRUE

resNew <- data.frame(Genotype=rownames(res),res,stringsAsFactors=FALSE)
 attr(resNew,"row.names") <- attr(res2,"row.names")
#[1] TRUE


So I'm having a problem with the part b of the assignment. 

This is the question 

This is how I solved 
Part A 

now for 
Part B 
Am i supposed to do something along the lines of 

sapply(tapply(Gene[ , ], Gene$Genotype), mean) 

This doesn't work.

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list