tag:blogger.com,1999:blog-2550386066385582083.post2686738126799289504..comments2017-08-30T02:37:58.701-07:00Comments on R for Public Health: The infamous apply functionSlawa Rokickihttp://www.blogger.com/profile/17017311098539878072noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-2550386066385582083.post-81271562013609185432015-07-10T06:25:54.524-07:002015-07-10T06:25:54.524-07:00I have this code I'm working with and it has s...I have this code I'm working with and it has so many for loops. I read your blog and wanted some help with my code. Here is a part of my code which is most confusing to deal with.<br /><br />calcobj_i <- function (X, iter, pa) <br />{ <br />ww=rep(0,pa$n)<br />for (k in 1:pa$K) {<br />w=iter$cw[k]*dmnorm(X,iter$center[k,],iter$Lambda[,,k]%*%t(iter$Lambda[,,k])+iter$Psi[,,k]) <br />w=w*(w>=0) <br />ww=w+ww <br />}}<br /><br />Here iter and pa are lists containing vectors and matrices like lambda, cw, center, and Psi with dimensions specified above and X is a matrix. dmnorm (mahalanobis also gives values) is the multivariate normal density function which gives a vector of values. Do not worry about them.<br />I need to know how to use apply on these type of functions. Help is much appreciated. Thanks. :)Ankit Jainhttps://www.blogger.com/profile/09977740864246022558noreply@blogger.comtag:blogger.com,1999:blog-2550386066385582083.post-90942273713387584322014-05-07T05:30:51.432-07:002014-05-07T05:30:51.432-07:00Sure, that would replace any 99 or -99 with the va...Sure, that would replace any 99 or -99 with the value in the ID column, and leave the other entries alone.Slawa Rokickihttps://www.blogger.com/profile/17017311098539878072noreply@blogger.comtag:blogger.com,1999:blog-2550386066385582083.post-55483204629597879952014-05-06T19:11:19.754-07:002014-05-06T19:11:19.754-07:00how to do if I want to replace the value of x from...how to do if I want to replace the value of x from ID column. Is it should be<br />newdata<-apply(originaldata[,c(2,4:6)], MARGIN=2, function(x) {ifelse(x==99 | x==-99, ID,x)}retinolhttps://www.blogger.com/profile/17975292940692785105noreply@blogger.com