An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090423/7a7e5983/attachment-0001.pl>
Generalized 2D list/array/whatever?
8 messages · Gabor Grothendieck, Toby, David Winsemius +2 more
Matrix made from a list:
m <- list(sin, 1:3, letters[1:3], expression(a+b))
dim(m) <- c(2, 2)
dimnames(m) <- list(letters[1:2], LETTERS[1:2])
class(m) # matrix
or
M <- structure(list(sin, 1:3, letters[1:3], expression(a+b)), .Dim = c(2, 2),
.Dimnames = list(c("a", "b"), c("A", "B")))
class(M) # matrix
On Thu, Apr 23, 2009 at 10:03 PM, Toby <tobias.weingartner at gmail.com> wrote:
I'm trying to figure out how I can get a generalized 2D list/array/matrix/whatever working. ?Seems I can't figure out how to make the variables the right type. ?I always seem to get some sort of error... out of bounds, wrong type, wrong dim, etc. Very confused... :) x[["some label", "some other index"]] <- 3 x[["some other label", "something else"]] <- 4 I don't know the indexes/label ahead of time... they get generated... ?Any thoughts? -Toby. ? ? ? ?[[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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090423/70a6290d/attachment-0001.pl>
I cannot figure out what you are trying to do, but I'm reasonably sure that the construction: xxx[[index,index2]] ... is going to fail. If you want to create a two dimensional structure, most people would use either a dataframe or a matrix rather than a list, although lists have the advantage that they do not need to be "pre-dimensioned". If you know what your dimension are in advance they are probably the way to go. If you want to access multiple "dimensions" of a list, do not use [[ , ]] but rather [[ idx ]] to get the desired first "dimension" and then [ idx2 ] to the right of the expression for the first "dimension" to get or change the second "dimension", although "level" or "depth" might be the better terminology. m <- list(sin, 1:3, letters[1:3], expression(a+b)) m[[2]][3] ] m[[2]][3] <- 8 m m[[2]][3] You have a better chance of getting on target advice if you explain what you want to do rather than showing us code patterned on another language that obviously doesn't work in R.
On Apr 23, 2009, at 10:29 PM, Toby wrote:
I must be going about my idea really backwards. I have functions
like so:
Nasa_PART_Bounds <- rbind(Nasa_PART_Bounds, c("Nasa_PART_B1"))
Nasa_PART_B1 <- function(l,u) {
l[["Nasa_PART_B1", "CYCLOMATIC_COMPLEXITY"]] <- 8
u[["Nasa_PART_B1", "CYCLOMATIC_COMPLEXITY"]] <- 60
l[["Nasa_PART_B1", "LOC_TOTAL"]] <- 73
}
Where I add the function names of each "bounds" function to a list,
which later on get called with two variables.
dispatch2 <- function(f, x, y) {
eval(call(f, x, y))
}
for (i in bounds) {
bb <- eval(substitute(foo[,1], list(foo=as.name(i))))
rr <- lapply(bb, dispatch2, lb, ub)
}
However, I don't know how to do this in a better "R" way. I think
my C and
pearl experience is showing through... :(
-Toby.
On Thu, Apr 23, 2009 at 8:20 PM, Gabor Grothendieck <ggrothendieck at gmail.com
wrote:
Matrix made from a list:
m <- list(sin, 1:3, letters[1:3], expression(a+b))
dim(m) <- c(2, 2)
dimnames(m) <- list(letters[1:2], LETTERS[1:2])
class(m) # matrix
or
M <- structure(list(sin, 1:3, letters[1:3], expression(a+b)), .Dim
= c(2,
2),
.Dimnames = list(c("a", "b"), c("A", "B")))
class(M) # matrix
On Thu, Apr 23, 2009 at 10:03 PM, Toby <tobias.weingartner at gmail.com>
wrote:
I'm trying to figure out how I can get a generalized 2D list/array/matrix/whatever working. Seems I can't figure out how to make the variables the right type. I always seem to get some sort of error... out of bounds, wrong type, wrong dim, etc. Very confused... :) x[["some label", "some other index"]] <- 3 x[["some other label", "something else"]] <- 4 I don't know the indexes/label ahead of time... they get generated...
Any
thoughts?
David Winsemius, MD Heritage Laboratories West Hartford, CT
On Apr 24, 2009, at 12:16 AM, David Winsemius wrote:
I cannot figure out what you are trying to do, but I'm reasonably sure that the construction: xxx[[index,index2]] ... is going to fail. If you want to create a two dimensional structure, most people would use either a dataframe or a matrix rather than a list, although lists have the advantage that they do not need to be "pre-dimensioned". If you know what your dimension are in advance they are probably the way to go. If you want to access multiple "dimensions" of a list, do not use [[ , ]] but rather [[ idx ]] to get the desired first "dimension" and then [ idx2 ] to the right of the expression for the first "dimension" to get or change the second "dimension", although "level" or "depth" might be the better terminology. m <- list(sin, 1:3, letters[1:3], expression(a+b)) m[[2]][3] # ]
# that "]" shouldn't be there
m[[2]][3] <- 8 m m[[2]][3] You have a better chance of getting on target advice if you explain what you want to do rather than showing us code patterned on another language that obviously doesn't work in R. On Apr 23, 2009, at 10:29 PM, Toby wrote:
I must be going about my idea really backwards. I have functions
like so:
Nasa_PART_Bounds <- rbind(Nasa_PART_Bounds, c("Nasa_PART_B1"))
Nasa_PART_B1 <- function(l,u) {
l[["Nasa_PART_B1", "CYCLOMATIC_COMPLEXITY"]] <- 8
u[["Nasa_PART_B1", "CYCLOMATIC_COMPLEXITY"]] <- 60
l[["Nasa_PART_B1", "LOC_TOTAL"]] <- 73
}
Where I add the function names of each "bounds" function to a list,
which later on get called with two variables.
dispatch2 <- function(f, x, y) {
eval(call(f, x, y))
}
for (i in bounds) {
bb <- eval(substitute(foo[,1], list(foo=as.name(i))))
rr <- lapply(bb, dispatch2, lb, ub)
}
However, I don't know how to do this in a better "R" way. I think
my C and
pearl experience is showing through... :(
-Toby.
On Thu, Apr 23, 2009 at 8:20 PM, Gabor Grothendieck <ggrothendieck at gmail.com
wrote:
Matrix made from a list:
m <- list(sin, 1:3, letters[1:3], expression(a+b))
dim(m) <- c(2, 2)
dimnames(m) <- list(letters[1:2], LETTERS[1:2])
class(m) # matrix
or
M <- structure(list(sin, 1:3, letters[1:3], expression(a+b)), .Dim
= c(2,
2),
.Dimnames = list(c("a", "b"), c("A", "B")))
class(M) # matrix
On Thu, Apr 23, 2009 at 10:03 PM, Toby
<tobias.weingartner at gmail.com>
wrote:
I'm trying to figure out how I can get a generalized 2D list/array/matrix/whatever working. Seems I can't figure out how to make the variables the right type. I always seem to get some sort of error... out of bounds, wrong type, wrong dim, etc. Very confused... :) x[["some label", "some other index"]] <- 3 x[["some other label", "something else"]] <- 4 I don't know the indexes/label ahead of time... they get generated...
Any
thoughts?
David Winsemius, MD Heritage Laboratories West Hartford, CT
Toby wrote:
I'm trying to figure out how I can get a generalized 2D list/array/matrix/whatever working. Seems I can't figure out how to make the variables the right type. I always seem to get some sort of error... out of bounds, wrong type, wrong dim, etc. Very confused... :) x[["some label", "some other index"]] <- 3 x[["some other label", "something else"]] <- 4 I don't know the indexes/label ahead of time... they get generated... Any thoughts?
What you have there is not legal syntax, but this would be:
x[[c("some label", "some other index")]] <- 3
This assumes that x is a list, and one of its entries is a list named
"some label". It will not create that entry, but it will create an
entry named "some other index", so you need
if (is.null(x[["some label"]])) x[["some label"]] <- list()
first. After executing this line and your first line above, you'll get
> x
$`some label`
$`some label`$`some other index`
[1] 3
Duncan Murdoch
On Fri, Apr 24, 2009 at 5:50 AM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
Toby wrote:
I'm trying to figure out how I can get a generalized 2D list/array/matrix/whatever working. ?Seems I can't figure out how to make the variables the right type. ?I always seem to get some sort of error... out of bounds, wrong type, wrong dim, etc. Very confused... :) x[["some label", "some other index"]] <- 3 x[["some other label", "something else"]] <- 4 I don't know the indexes/label ahead of time... they get generated... ?Any thoughts?
What you have there is not legal syntax, but this would be:
It isn't?
a <- as.list(1:4)
dim(a) <- c(2, 2)
rownames(a) <- c("a", "b")
colnames(a) <- c("c", "d")
a[["a", "d"]]
Hadley
On 24/04/2009 8:48 AM, hadley wickham wrote:
On Fri, Apr 24, 2009 at 5:50 AM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
Toby wrote:
I'm trying to figure out how I can get a generalized 2D list/array/matrix/whatever working. Seems I can't figure out how to make the variables the right type. I always seem to get some sort of error... out of bounds, wrong type, wrong dim, etc. Very confused... :) x[["some label", "some other index"]] <- 3 x[["some other label", "something else"]] <- 4 I don't know the indexes/label ahead of time... they get generated... Any thoughts?
What you have there is not legal syntax, but this would be:
It isn't?
You're right, it is. Sorry. However, I think the solution I gave (not to make the list into an array, but rather to use a list of lists) is probably better in a lot of situations where the indices show up unpredictably. Duncan Murdoch
a <- as.list(1:4)
dim(a) <- c(2, 2)
rownames(a) <- c("a", "b")
colnames(a) <- c("c", "d")
a[["a", "d"]]
Hadley