Chris Long
2007-Oct-05 21:55 UTC
[R] Replacing NA values when building matrix using tapply
Hi, I'm building a matrix m from a data frame d which includes the matrix row, column and value. This works well enough: m <- tapply(d[,"value"],d[,c("row","column")],c) However, I'd like to replace any missing values with 0, not NA. The obvious doesn't work, however: m <- tapply(d[,"value"],d[,c("row","column")],function(x) {ifelse(is.na(x),0.0,x)}) I can always do this: m <- tapply(d[,"value"],d[,c("row","column")],c) m[is.na(m)] <- 0.0 But it's wasteful to process the table again. Any ideas? -- Christopher D. Long, San Diego Padres, 100 Park Boulevard, San Diego CA Score: 0, Diff: 1, clong killed by a Harvard Math Team on 1
Gabor Grothendieck
2007-Oct-06 00:56 UTC
[R] Replacing NA values when building matrix using tapply
Try this: xtabs(value ~ row + column, d)[] On 10/5/07, Chris Long <galizur at gmail.com> wrote:> Hi, > > I'm building a matrix m from a data frame d which includes the matrix row, > column and value. > > This works well enough: > > m <- tapply(d[,"value"],d[,c("row","column")],c) > > However, I'd like to replace any missing values with 0, not NA. The > obvious doesn't work, however: > > m <- tapply(d[,"value"],d[,c("row","column")],function(x) {ifelse(is.na(x),0.0,x)}) > > I can always do this: > > m <- tapply(d[,"value"],d[,c("row","column")],c) > m[is.na(m)] <- 0.0 > > But it's wasteful to process the table again. Any ideas? > -- > Christopher D. Long, San Diego Padres, 100 Park Boulevard, San Diego CA > > Score: 0, Diff: 1, clong killed by a Harvard Math Team on 1 > > ______________________________________________ > 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. >
Hello all, I have a vector with 1000 values and I would like to generate other correlated vector, but with different correlation coefficient (for example, r = 0.7). Any ideas how can I do this? Regards, Gustavo. _______________________________________________________ Experimente j? e veja as novidades.
joris.dewolf at cropdesign.com
2007-Oct-07 08:04 UTC
[R] Simulate data based on correlation coefficient
Try this: rnormcor <- function(x,rho) rnorm(1,rho*x,sqrt(1-rho^2)) a <- rnorm(1000,0,1) b <- sapply(a, rnormcor, rho = 0.7) cor(a,b) Joris "Gustavo" <gugarl1980 at yahoo .com.br> To Sent by: <r-help at r-project.org> r-help-bounces at r- cc project.org Subject [R] Simulate data based on 06/10/2007 04:48 correlation coefficient Hello all, I have a vector with 1000 values and I would like to generate other correlated vector, but with different correlation coefficient (for example, r = 0.7). Any ideas how can I do this? Regards, Gustavo. _______________________________________________________ Experimente j? e veja as novidades. ______________________________________________ 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.
Similar questions have been asked here before. See http://finzi.psych.upenn.edu/R/Rhelp02a/archive/110042.html for one possible answer (your case will be simpler than this). -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Gustavo > Sent: Friday, October 05, 2007 8:48 PM > To: r-help at r-project.org > Subject: [R] Simulate data based on correlation coefficient > > Hello all, > > > > I have a vector with 1000 values and I would like to generate > other correlated vector, but with different correlation > coefficient (for example, r = 0.7). > > Any ideas how can I do this? > > > > Regards, > > Gustavo. > > > > > > > _______________________________________________________ > > Experimente j? e veja as novidades. > > ______________________________________________ > 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. >