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 are 0, or more than 1 rows returned the result is a
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
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
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
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,
Dear List,
I have been trying to extract associations from a matrix whereby individual
locations are within a certain distance threshold from one another.
I have been able to extract those individuals where there is 'no interaction'
(i.e. where these individuals are not within a specified
#Hallo again.. Thank you for your answers. To sum up:
#The problem was that we have the matrix m
m-matrix(numeric(length=5*4),nrow=5,ncol=4)
m
# [,1] [,2] [,3] [,4]
# [1,]0000
# [2,]0000
# [3,]0000
# [4,]0000
# [5,]00
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
0 0 0 0
0 0 0 0
0
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,
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
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 00.2
[3,]0.300
How can I do it efficiently? Thanks.
--
Best,
Zhenjiang
[[alternative
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
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
-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
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:
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,
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
return the matrix:
1 0 0 0
2 1 0 0
0 2 1 0
0 0 2 1
0 0 0 2
Thanks very much for any
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
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
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
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(
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
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),
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
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
Hi,
Can any one please explain why the following code doesn't work? Or can anyone
suggest an alternative.
Suppose
x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
mat-0;
for(j in 1:length(x))
{
for(i in 1:p)
mat[i,j]-x[j]^i;
}
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
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
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)
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
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
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 vector
m
[,1] [,2] [,3]
[1,]
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
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
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,
35 matches
Mail list logo