An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080923/c521f34c/attachment.pl>
Generalising to n-dimensions
7 messages · robin hankin, Laura Bonnett
First bit: > x <- c(3,2,2) > expand.grid(sapply(x,seq_len)) Var1 Var2 Var3 1 1 1 1 2 2 1 1 3 3 1 1 4 1 2 1 5 2 2 1 6 3 2 1 7 1 1 2 8 2 1 2 9 3 1 2 10 1 2 2 11 2 2 2 12 3 2 2 > second bit I'm not sure about. I didn't quite get why d=2 implied the order is 2,1. Could you post a small self-contained example? HTH rksh
Laura Bonnett wrote:
Hi R-helpers, I have two queries relating to generalising to n dimensions: What I want to do in the first one is generalise the following statement: expand<-expand.grid(1:x[1],1:x[2],...1:x[n]) where x is a vector of integers and expand.grid gives every combination of the set of numbers, so for example, expand.grid(1:2, 1:3) takes 1,2 and 1,2,3 and gives 1,1 2,1 1,2 2,2 1,3 2,3 My x vector has varying lengths and I can't find a way of giving it every set without stating each set individually. Secondly and similarly, I want to get the table within crosstable that has the elements defined by the combinations given in expand above crosstable[,,expand[d,1],expand[d,2],expand[d,3],...expand[d,n]] where crosstable is just a crosstabulation of an n+2-dimensional dataset and I am trying to pick out those that are in combination 'd' of expand. So for example, using x[1]=2 and x[2]=3 as above example, if d =2 then the order is 2,1 so I take crosstable[,,2,1]. Can anyone suggest a way to give the code every set without stating each set individually? Thank you [[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.
Robin K. S. Hankin Senior Research Associate Cambridge Centre for Climate Change Mitigation Research (4CMR) Department of Land Economy University of Cambridge rksh1 at cam.ac.uk 01223-764877
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080923/4cdd32ce/attachment.pl>
Laura Bonnett wrote:
crosstable[,,expand[d,1],expand[d,2],expand[d,3],...expand[d,n]] crosstable is just a crosstabulation of an n+2-dimensional dataset and I am trying to pick out those that are in combination 'd' of expand. So for example, for 5-dimensional data using your example: Var1 Var2 Var3 1 1 1 1 2 2 1 1 3 3 1 1 4 1 2 1 5 2 2 1 6 3 2 1 7 1 1 2 8 2 1 2 9 3 1 2 10 1 2 2 11 2 2 2 12 3 2 2 d refers to the row of the matrix above - d=2 is 2,1,1 so crosstable[,,2,1,1] would retrieve all the data where Var1 =2, Var2=1, Var3=1 and the two remaining variables are given in the crosstabulations for all values. Is that any better?
OK I think I understand. The magic package uses this type of
construction extensively, but not this particular one.
It's trickier than I'd have expected.
Try this:
f <- function(a,v){
jj <-
sapply(dim(a)[seq_len(length(dim(a))-length(v))],seq_len,simplify=FALSE)
jj <- c(jj , as.list(v))
do.call("[" , c(list(a) , jj, drop=TRUE))
}
[you will have to coerce the output from expand.grid() to a matrix in
order to extract a row from it]
HTH
rksh
Robin K. S. Hankin Senior Research Associate Cambridge Centre for Climate Change Mitigation Research (4CMR) Department of Land Economy University of Cambridge rksh1 at cam.ac.uk 01223-764877
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080924/fd65888c/attachment.pl>
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080926/da23b1c1/attachment.pl>
Laura Bonnett wrote:
Sorry to hassle you, but I really need to get my code up and running. Please can you therefore explain what a and v are?
Hi Laura. I've been away (in Norwich). Sorry not to give an example.
Variable 'a' is an array and variable 'd' is the same as in your
original email.
> a <- array(1:4,c(3,2,2,4))
> d <- c(1,2)
> f(a,d)
[,1] [,2]
[1,] 1 4
[2,] 2 1
[3,] 3 2
>
Thus in this case f(a,d) gives a[,,1,2]. Function f() is necessary because
you specified that the length of 'd' is not known in advance.
You can get 'd' from a single row of expand.grid() [but you will have to
coerce it to a matrix]
HTH
rksh
Thank you,
Laura
On Wed, Sep 24, 2008 at 8:27 PM, Laura Bonnett
<l.j.bonnett at googlemail.com <mailto:l.j.bonnett at googlemail.com>> wrote:
Can I ask what a and v are?
Thanks,
Laura
On Sat, Aug 23, 2008 at 11:41 AM, Robin Hankin <rksh1 at cam.ac.uk
<mailto:rksh1 at cam.ac.uk>> wrote:
Laura Bonnett wrote:
crosstable[,,expand[d,1],expand[d,2],expand[d,3],...expand[d,n]]
crosstable is just a crosstabulation of an
n+2-dimensional dataset and I am trying to pick out those
that are in combination 'd' of expand. So for example, for
5-dimensional data using your example:
Var1 Var2 Var3
1 1 1 1
2 2 1 1
3 3 1 1
4 1 2 1
5 2 2 1
6 3 2 1
7 1 1 2
8 2 1 2
9 3 1 2
10 1 2 2
11 2 2 2
12 3 2 2
d refers to the row of the matrix above - d=2 is 2,1,1 so
crosstable[,,2,1,1] would retrieve all the data where Var1
=2, Var2=1, Var3=1 and the two remaining variables are
given in the crosstabulations for all values.
Is that any better?
OK I think I understand. The magic package uses this type of
construction extensively, but not this particular one.
It's trickier than I'd have expected.
Try this:
f <- function(a,v){
jj <-
sapply(dim(a)[seq_len(length(dim(a))-length(v))],seq_len,simplify=FALSE)
jj <- c(jj , as.list(v))
do.call("[" , c(list(a) , jj, drop=TRUE))
}
[you will have to coerce the output from expand.grid() to a
matrix in order to extract a row from it]
HTH
rksh
--
Robin K. S. Hankin
Senior Research Associate
Cambridge Centre for Climate Change Mitigation Research (4CMR)
Department of Land Economy
University of Cambridge
rksh1 at cam.ac.uk <mailto:rksh1 at cam.ac.uk>
01223-764877
Robin K. S. Hankin Senior Research Associate Cambridge Centre for Climate Change Mitigation Research (4CMR) Department of Land Economy University of Cambridge rksh1 at cam.ac.uk 01223-764877