[1] 86 90 185 196 197 209 210 215 216 217 218
#Here is a matrix. The rows and columns correspond to the IDs in cwaves, and
the matrix is populated with a coefficient
[1] 0.0625
But I want to use cwaves[4] and cwaves[10] to get the name, because this is
part of an iteration through thousands of IDs.
This didn't work, of course, because it tries to pull out mat[196,217] which
doesn't exist.
mat[cwaves[4], cwaves[10]]
Error: subscript out of bounds
mat["cwaves[4]", "cwaves[10]"]
Error: subscript out of bounds
I also tried to put the name in a variable to then use as the index, and the
same thing happens, of course.
[1] 86 90 185 196 197 209 210 215 216 217 218
#Here is a matrix. The rows and columns correspond to the IDs in cwaves, and
the matrix is populated with a coefficient
[1] 0.0625
But I want to use cwaves[4] and cwaves[10] to get the name, because this is
part of an iteration through thousands of IDs.
This didn't work, of course, because it tries to pull out mat[196,217] which
doesn't exist.
mat[cwaves[4], cwaves[10]]
Error: subscript out of bounds
mat["cwaves[4]", "cwaves[10]"]
Error: subscript out of bounds
I also tried to put the name in a variable to then use as the index, and the
same thing happens, of course.
a <- cwaves[4]
b <- cwaves[10]
mat[a,b]
Error: subscript out of bounds
mat["a","b"]
Error: subscript out of bounds
Is it possible to do this? I hope the way I language it makes sense.
Turn cwaves into a vector of characters:
cwaves <- as.character(cwaves)
Now you should be able to index like this: mat[cwaves[4], cwaves[10]]
Berend
Hi,
May be this helps:
cwaves<-c(86,90,185,196,197,209,210,215,216,217,218,1162,1323,1338,1709)
?cwaves1<-as.character(cwaves)
mat[cwaves1[4],cwaves1[7]]
#[1] 0.0625
?mat[cwaves1[4],cwaves1[10]]
#[1] 0.03125
A.K.
----- Original Message -----
From: AHJ <ahadjixenofontos at med.miami.edu>
To: r-help at r-project.org
Cc:
Sent: Monday, October 15, 2012 1:57 PM
Subject: [R] Referring to matrix elements by name, iteratively
#Here is a vector of IDs
cwaves
[1]? ? 86? ? 90? 185? 196? 197? 209? 210? 215? 216? 217? 218
#Here is a matrix. The rows and columns correspond to the IDs in cwaves, and
the matrix is populated with a coefficient
[1] 0.0625
But I want to use cwaves[4] and cwaves[10] to get the name, because this is
part of an iteration through thousands of IDs.
This didn't work, of course, because it tries to pull out mat[196,217] which
doesn't exist.
mat[cwaves[4], cwaves[10]]
Error: subscript out of bounds
mat["cwaves[4]", "cwaves[10]"]
Error: subscript out of bounds
I also tried to put the name in a variable to then use as the index, and the
same thing happens, of course.
Actually the rows and columns do not correspond to the IDs in cwaves since
rownames 1162, 1323, 1338, and 1709 do not appear in cwaves and there is no
column 197 in mat. If cwaves is defined as equal to colnames(mat), you will
get one definition, but it will not match cwaves defined as equal to
rownames(mat).
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Berend Hasselman
Sent: Monday, October 15, 2012 2:51 PM
To: AHJ
Cc: r-help at r-project.org
Subject: Re: [R] Referring to matrix elements by name, iteratively
On 15-10-2012, at 19:57, AHJ wrote:
#Here is a vector of IDs
cwaves
[1] 86 90 185 196 197 209 210 215 216 217 218
#Here is a matrix. The rows and columns correspond to the IDs in
Actually the rows and columns do not correspond to the IDs in cwaves since
rownames 1162, 1323, 1338, and 1709 do not appear in cwaves and there is no
column 197 in mat.
I didn't see that. I just looked at what the OP did.
If cwaves is defined as equal to colnames(mat), you will
get one definition, but it will not match cwaves defined as equal to
rownames(mat).
Then the OP could use cwaves for the column names and rwaves for the rownames.
As long as both are character vectors.
Berend
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Berend Hasselman
Sent: Monday, October 15, 2012 2:51 PM
To: AHJ
Cc: r-help at r-project.org
Subject: Re: [R] Referring to matrix elements by name, iteratively
On 15-10-2012, at 19:57, AHJ wrote:
#Here is a vector of IDs
cwaves
[1] 86 90 185 196 197 209 210 215 216 217 218
#Here is a matrix. The rows and columns correspond to the IDs in