#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> mat86 90 185 196 209 210 215 216 217 218 86 0 0 0 0 0 0.00000 0 0.000000 0.000000 0.000000 90 0 0 0 0 0 0.00000 0 0.000000 0.000000 0.000000 185 0 0 0 0 0 0.00000 0 0.062500 0.000000 0.015625 196 0 0 0 0 0 0.06250 0 0.000000 0.031250 0.000000 197 0 0 0 0 0 0.06250 0 0.000000 0.000000 0.000000 209 0 0 0 0 0 0.00000 0 0.000000 0.062500 0.000000 210 0 0 0 0 0 0.00000 0 0.000000 0.062500 0.000000 215 0 0 0 0 0 0.00000 0 0.000000 0.031250 0.000000 216 0 0 0 0 0 0.00000 0 0.000000 0.000000 0.000000 217 0 0 0 0 0 0.03125 0 0.031250 0.000000 0.000000 218 0 0 0 0 0 0.00000 0 0.000000 0.000000 0.031250 1162 0 0 0 0 0 0.00000 0 0.003906 0.007812 0.015625 1323 0 0 0 0 0 0.00000 0 0.007812 0.007812 0.000000 1338 0 0 0 0 0 0.00000 0 0.000000 0.000000 0.003906 1709 0 0 0 0 0 0.00000 0 0.000000 0.000000 0.000000> dput(mat)structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0625, 0.0625, 0, 0, 0, 0, 0.03125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0625, 0, 0, 0, 0, 0, 0, 0.03125, 0, 0.003906, 0.007812, 0, 0, 0, 0, 0, 0.03125, 0, 0.0625, 0.0625, 0.03125, 0, 0, 0, 0.007812, 0.007812, 0, 0, 0, 0, 0.015625, 0, 0, 0, 0, 0, 0, 0, 0.03125, 0.015625, 0, 0.003906, 0), .Dim = c(15L, 10L), .Dimnames = list(c("86", "90", "185", "196", "197", "209", "210", "215", "216", "217", "218", "1162", "1323", "1338", "1709" ), c("86", "90", "185", "196", "209", "210", "215", "216", "217", "218"))) #I know I can refer to element [4,6] in two ways, with the index, or with the name> mat[4,6][1] 0.0625> mat["196","210"][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. Thank you :) -- View this message in context: http://r.789695.n4.nabble.com/Referring-to-matrix-elements-by-name-iteratively-tp4646264.html Sent from the R help mailing list archive at Nabble.com.
Berend Hasselman
2012-Oct-15 19:51 UTC
[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 cwaves, and > the matrix is populated with a coefficient > >> mat > 86 90 185 196 209 210 215 216 217 218 > 86 0 0 0 0 0 0.00000 0 0.000000 0.000000 0.000000 > 90 0 0 0 0 0 0.00000 0 0.000000 0.000000 0.000000 > 185 0 0 0 0 0 0.00000 0 0.062500 0.000000 0.015625 > 196 0 0 0 0 0 0.06250 0 0.000000 0.031250 0.000000 > 197 0 0 0 0 0 0.06250 0 0.000000 0.000000 0.000000 > 209 0 0 0 0 0 0.00000 0 0.000000 0.062500 0.000000 > 210 0 0 0 0 0 0.00000 0 0.000000 0.062500 0.000000 > 215 0 0 0 0 0 0.00000 0 0.000000 0.031250 0.000000 > 216 0 0 0 0 0 0.00000 0 0.000000 0.000000 0.000000 > 217 0 0 0 0 0 0.03125 0 0.031250 0.000000 0.000000 > 218 0 0 0 0 0 0.00000 0 0.000000 0.000000 0.031250 > 1162 0 0 0 0 0 0.00000 0 0.003906 0.007812 0.015625 > 1323 0 0 0 0 0 0.00000 0 0.007812 0.007812 0.000000 > 1338 0 0 0 0 0 0.00000 0 0.000000 0.000000 0.003906 > 1709 0 0 0 0 0 0.00000 0 0.000000 0.000000 0.000000 > >> dput(mat) > structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0625, > 0.0625, 0, 0, 0, 0, 0.03125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0625, 0, 0, 0, 0, 0, 0, 0.03125, > 0, 0.003906, 0.007812, 0, 0, 0, 0, 0, 0.03125, 0, 0.0625, 0.0625, > 0.03125, 0, 0, 0, 0.007812, 0.007812, 0, 0, 0, 0, 0.015625, 0, > 0, 0, 0, 0, 0, 0, 0.03125, 0.015625, 0, 0.003906, 0), .Dim = c(15L, > 10L), .Dimnames = list(c("86", "90", "185", "196", "197", "209", > "210", "215", "216", "217", "218", "1162", "1323", "1338", "1709" > ), c("86", "90", "185", "196", "209", "210", "215", "216", "217", > "218"))) > > #I know I can refer to element [4,6] in two ways, with the index, or with > the name > >> mat[4,6] > [1] 0.0625 >> mat["196","210"] > [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> mat? ? 86 90 185 196 209? ? 210 215? ? ? 216? ? ? 217? ? ? 218 86? ? 0? 0? 0? 0? 0 0.00000? 0 0.000000 0.000000 0.000000 90? ? 0? 0? 0? 0? 0 0.00000? 0 0.000000 0.000000 0.000000 185? 0? 0? 0? 0? 0 0.00000? 0 0.062500 0.000000 0.015625 196? 0? 0? 0? 0? 0 0.06250? 0 0.000000 0.031250 0.000000 197? 0? 0? 0? 0? 0 0.06250? 0 0.000000 0.000000 0.000000 209? 0? 0? 0? 0? 0 0.00000? 0 0.000000 0.062500 0.000000 210? 0? 0? 0? 0? 0 0.00000? 0 0.000000 0.062500 0.000000 215? 0? 0? 0? 0? 0 0.00000? 0 0.000000 0.031250 0.000000 216? 0? 0? 0? 0? 0 0.00000? 0 0.000000 0.000000 0.000000 217? 0? 0? 0? 0? 0 0.03125? 0 0.031250 0.000000 0.000000 218? 0? 0? 0? 0? 0 0.00000? 0 0.000000 0.000000 0.031250 1162? 0? 0? 0? 0? 0 0.00000? 0 0.003906 0.007812 0.015625 1323? 0? 0? 0? 0? 0 0.00000? 0 0.007812 0.007812 0.000000 1338? 0? 0? 0? 0? 0 0.00000? 0 0.000000 0.000000 0.003906 1709? 0? 0? 0? 0? 0 0.00000? 0 0.000000 0.000000 0.000000> dput(mat)structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0625, 0.0625, 0, 0, 0, 0, 0.03125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0625, 0, 0, 0, 0, 0, 0, 0.03125, 0, 0.003906, 0.007812, 0, 0, 0, 0, 0, 0.03125, 0, 0.0625, 0.0625, 0.03125, 0, 0, 0, 0.007812, 0.007812, 0, 0, 0, 0, 0.015625, 0, 0, 0, 0, 0, 0, 0, 0.03125, 0.015625, 0, 0.003906, 0), .Dim = c(15L, 10L), .Dimnames = list(c("86", "90", "185", "196", "197", "209", "210", "215", "216", "217", "218", "1162", "1323", "1338", "1709" ), c("86", "90", "185", "196", "209", "210", "215", "216", "217", "218"))) #I know I can refer to element [4,6] in two ways, with the index, or with the name> mat[4,6][1] 0.0625> mat["196","210"][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. Thank you :) -- View this message in context: http://r.789695.n4.nabble.com/Referring-to-matrix-elements-by-name-iteratively-tp4646264.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Thank you both for the suggestion. So this means that when the vector is one of characters the string inside it is interpreted as itself, rather than as an index. That makes so much sense! Thanks again :) -- View this message in context: http://r.789695.n4.nabble.com/Referring-to-matrix-elements-by-name-iteratively-tp4646264p4646391.html Sent from the R help mailing list archive at Nabble.com.
Reasonably Related Threads
- R optim(method="L-BFGS-B"): unexpected behavior when working with parent environments
- stringsAsFactors has no impact in expand.grid()?
- R optim(method="L-BFGS-B"): unexpected behavior when working with parent environments
- one (small) sample wilcox.test confidence intervals
- R optim(method="L-BFGS-B"): unexpected behavior when working with parent environments