Hi, I have a file like this: 1 2 0.1 2 3 0.2 3 1 0.3 And I want to read it to create a matrix like this: [,1] [,2] [,3] [1,] 0 0.1 0 [2,] 0 0 0.2 [3,] 0.3 0 0 How can I do it efficiently? Thanks. -- Best, Zhenjiang [[alternative HTML version deleted]]
Try this: Lines <- '1 2 0.1 2 3 0.2 3 1 0.3' DF <- read.table(textConnection(Lines)) m <- matrix(0, ncol = nrow(DF), nrow = nrow(DF)) m[as.matrix(DF[1:2])] <- DF[[3]] On Tue, Aug 10, 2010 at 3:03 PM, zhenjiang xu <zhenjiang.xu@gmail.com>wrote:> Hi, > > I have a file like this: > 1 2 0.1 > 2 3 0.2 > 3 1 0.3 > > And I want to read it to create a matrix like this: > [,1] [,2] [,3] > [1,] 0 0.1 0 > [2,] 0 0 0.2 > [3,] 0.3 0 0 > > How can I do it efficiently? Thanks. > -- > Best, > Zhenjiang > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of zhenjiang xu > Sent: Tuesday, August 10, 2010 11:03 AM > To: R-help at r-project.org > Subject: [R] matrix problem > > Hi, > > I have a file like this: > 1 2 0.1 > 2 3 0.2 > 3 1 0.3 > > And I want to read it to create a matrix like this: > [,1] [,2] [,3] > [1,] 0 0.1 0 > [2,] 0 0 0.2 > [3,] 0.3 0 0 > > How can I do it efficiently? Thanks.Use a k-column matrix as a subscript into your k-dimensional output array. (k is 2 in your case.) E.g., 'input' is your matrix in a form that one can paste into an R session: > input <- cbind(c(1,2,3), c(2,3,1), c(.1,.2,.3)) > size <- max(input[,1:2]) # you may want something else here > output <- matrix(0.0, size, size) > output[input[,1:2]] <- input[,3] > output [,1] [,2] [,3] [1,] 0.0 0.1 0.0 [2,] 0.0 0.0 0.2 [3,] 0.3 0.0 0.0 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -- > Best, > Zhenjiang > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Let me give you a not that efficient one... assume you have read the matrix (named as x) into R: n=dim(x)[1] y=matrix(0,n,n) for (i in 1:n) y[x[i,1],x[i,2]]=x[i,3] -- View this message in context: http://r.789695.n4.nabble.com/matrix-problem-tp2320193p2320219.html Sent from the R help mailing list archive at Nabble.com.
Hi, I guess you just want to reshape your data to wide format. strs <- "Index Time Value 1 2 0.1 2 3 0.2 3 1 0.3" DF <- read.table(textConnection(strs),header=T) rDF <- reshape(DF, idvar="Index", timevar="Time", direction="wide") rDF[is.na(rDF)] <- 0 ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/matrix-problem-tp2320193p2320287.html Sent from the R help mailing list archive at Nabble.com.