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

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


Hi,
Try:

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

A.K.

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:
Hi,

(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))

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


#or
library(plyr)
 res3 <- ddply(Gene,.(Genotype),numcolwise(mean))


identical(res2,res3)
#[1] TRUE


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


A.K.


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
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.



More information about the R-help mailing list