Re: [R] Matrix problem

2012-04-10 Thread David Winsemius
On Apr 10, 2012, at 7:33 PM, Worik R wrote: Friends I am extracting sub-sets of the rows of a matrix. Generally the result is a matrix. But there is a special case. When the result returned is a single row it is returned as a vector (in the example below an integer vector). If there

Re: [R] Matrix problem

2012-04-10 Thread Worik R
Thank you. That was exactly what I need. Looking at '?[' I see... drop: For matrices and arrays. If ‘TRUE’ the result is coerced to the lowest possible dimension (see the examples). This only works for extracting elements, not for the replacement. See ‘drop’

Re: [R] Matrix problem

2012-04-10 Thread David Winsemius
On Apr 10, 2012, at 8:01 PM, Worik R wrote: Thank you. That was exactly what I need. Looking at '?[' I see... drop: For matrices and arrays. If ‘TRUE’ the result is coerced to the lowest possible dimension (see the examples). This only works for extracting

Re: [R] Matrix problem to extract animal associations

2012-02-27 Thread ilai
set.seed(1) (DFid - data.frame( x = sample(1:20,10), y = sample(1:20,10), IDs = sapply(1:10,function(i) paste(ID,i,sep= require(spdep) coordinates(DFid) - ~x+y coords - coordinates(DFid) dnn4 - dnearneigh(DFid,0,4) summary(dnn4) plot(DFid) plot(dnn4,coords,add=T,col=2) nb2mat(dnn4,

Re: [R] matrix problem

2011-06-20 Thread Sarah Goslee
How about: y - c(1,1,1,3,2) m - matrix(0, nrow=length(y), ncol=4) m[y==1, ] - matrix(1:4, nrow=sum(y == 1), ncol=4, byrow=TRUE) or, depending on your actual problem y - c(1,1,1,3,2) m - matrix(0, nrow=length(y), ncol=4) m[y == 1,] - col(m[y == 1,]) Sarah On Mon, Jun 20, 2011 at 3:54 PM,

Re: [R] matrix problem

2011-06-20 Thread David Winsemius
On Jun 20, 2011, at 3:54 PM, Costis Ghionnis wrote: Hallo everyone! I have a problem about creating a matrix... Suppose we have a vector y-c(1,1,1,3,2) and a zero matrix, m ,with nrows=length(y) and ncol=4. The matrix would look like this: 0 0 0 0 0 0 0 0

Re: [R] matrix problem

2010-08-10 Thread Henrique Dallazuanna
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...@gmail.comwrote: Hi, I have a file like this: 1 2 0.1 2 3 0.2 3 1

Re: [R] matrix problem

2010-08-10 Thread Xia.Li
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

Re: [R] matrix problem

2010-08-10 Thread William Dunlap
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of zhenjiang xu Sent: Tuesday, August 10, 2010 11:03 AM To: R-help@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

Re: [R] matrix problem

2010-08-10 Thread Wu Gong
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:

Re: [R] matrix problem

2009-07-05 Thread William Simpson
Thanks everyone for your help! Bill __ 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,

Re: [R] matrix problem

2009-07-04 Thread Henrique Dallazuanna
Try this: m - matrix(0, nrow = 5, ncol = 4) diag(m) - x[1] diag(m[-1,]) - x[2] On Sat, Jul 4, 2009 at 12:17 PM, William Simpson william.a.simp...@gmail.com wrote: Can anybody please tell me a good way to do the following? Given a vector, number of rows and number of columns, return a matrix

Re: [R] matrix problem

2009-07-04 Thread David Winsemius
On Jul 4, 2009, at 11:17 AM, William Simpson wrote: Can anybody please tell me a good way to do the following? Given a vector, number of rows and number of columns, return a matrix as follows. Easiest to give an example: x=c(1,2), nrow=5, ncol=4 You ought to separate those assignments with

Re: [R] matrix problem

2009-07-04 Thread William Simpson
Thanks everyone for the help. I should have said that I want to do this generally, not as a one-off. So I want a function to do it. Like this tp-function(x, nr, nc) { matrix( c(x,rep(0, nr-length(x)+1)), nrow=nr, ncol=nc) } tp(x=c(1,2), nr=5, nc=4) This one looks good -- the warning message

Re: [R] matrix problem

2009-07-04 Thread Charles C. Berry
On Sat, 4 Jul 2009, William Simpson wrote: Can anybody please tell me a good way to do the following? Not sure how you want cases like x = 1:3, nrow=2 , ncol=7 to be handled, but for the example you give, this works: mat - matrix(0,nr=nrow,nc=ncol) indx - outer( seq(

Re: [R] matrix problem

2009-07-04 Thread Jorge Ivan Velez
Dear William, Here is one way using Henrique's solution: Make - function(x, nR, nC){ m - matrix(0, nrow = nR, ncol = nC) diag(m) - x[1] diag(m[-1,]) - x[2] m } Make(x = c(1,2), nR = 5, nC = 4) HTH, Jorge On Sat, Jul 4, 2009 at 11:59 AM, William Simpson

Re: [R] matrix problem

2009-07-04 Thread David Winsemius
On Jul 4, 2009, at 11:59 AM, William Simpson wrote: Thanks everyone for the help. I should have said that I want to do this generally, not as a one-off. So I want a function to do it. Like this tp-function(x, nr, nc) { matrix( c(x,rep(0, nr-length(x)+1)), nrow=nr, ncol=nc) } tp(x=c(1,2),

Re: [R] matrix problem

2009-07-04 Thread William Simpson
Doesn't work: Make(x=c(2,1,1,1),nR=5,nC=2) [,1] [,2] [1,]20 [2,]12 [3,]01 [4,]00 [5,]00 should be [,1] [,2] [1,]20 [2,]12 [3,]11 [4,]11 [5,]01 On Sat, Jul 4, 2009 at 5:04 PM, Jorge Ivan

Re: [R] matrix problem

2009-07-04 Thread Henrique Dallazuanna
Try this: foo - function(x, nrow, ncol){ m - matrix(0, nrow = nrow, ncol = ncol) m[cbind(unlist(lapply(0:(ncol - 1), `+`, seq(x))),rep(1:ncol, each = length(x)))] - x m } foo(c(2, 1, 1, 1), nrow = 5, ncol = 2) On Sat, Jul 4, 2009 at 1:26 PM, William Simpson

Re: [R] Matrix: Problem with the code

2009-01-09 Thread Sarah Goslee
Well, mat doesn't have any dimensions / isn't a matrix, and we don't know what p is supposed to be. But leaving aside those little details, do you perhaps want something like this: x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3) p - 5 mat- matrix(0, nrow=p, ncol=length(x)) for(j

Re: [R] Matrix: Problem with the code

2009-01-09 Thread Charlotte Wickham
One of those more elegant ways: outer(x, 1:p, ^) Charlotte On Fri, Jan 9, 2009 at 4:24 PM, Sarah Goslee sarah.gos...@gmail.com wrote: Well, mat doesn't have any dimensions / isn't a matrix, and we don't know what p is supposed to be. But leaving aside those little details, do you perhaps want

Re: [R] Matrix: Problem with the code

2009-01-09 Thread markleeds
Charlotte: I ran your code because I wasn't clear on it and your way would cause more matrices than the person requested. So I think the code below it, although not too short, does what the person asked. Thanks though because I understand outer better now. temp - matrix(c(1,2,3,4,5,6),ncol=2)

Re: [R] Matrix: Problem with the code

2009-01-09 Thread Kingsford Jones
On Fri, Jan 9, 2009 at 6:36 PM, markle...@verizon.net wrote: Charlotte: I ran your code because I wasn't clear on it and your way would cause more matrices than the person requested. Bhargab gave us x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3) and said: I want to have a matrix with p

Re: [R] Matrix: Problem with the code

2009-01-09 Thread markleeds
Thanks Kingsford. I thought the column power was supposed to be just for that column but you're probably correct. English has its oddities because if one reads the actual sentence the person wrote it's still not clear, atleast to me. Actually I want to have a matrix with p columns such that

Re: [R] matrix problem

2008-04-21 Thread Chuck Cleland
On 4/21/2008 5:54 AM, William Simpson wrote: Hi Everyone, I am running into a problem with matrices. I use R version 2.4.1 and an older version. The problem is this: m-matrix(ncol=3,nrow=4) m[,1:3]-runif(n=4) That does what I expect; it fills up the rows of the matrix with the data

Re: [R] matrix problem

2008-04-21 Thread Rolf Turner
On 21/04/2008, at 9:54 PM, William Simpson wrote: Hi Everyone, I am running into a problem with matrices. I use R version 2.4.1 and an older version. The problem is this: m-matrix(ncol=3,nrow=4) m[,1:3]-runif(n=4) That does what I expect; it fills up the rows of the matrix with the

Re: [R] matrix problem

2008-04-21 Thread William Simpson
Thanks very much Petr and Rold for your helpful replies. Cheers Bill __ 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,