[R] Add a dim to an array
Dear list, I'm trying to add a new dim to a multidimensional array. My array looks like this a1 - array(1:8, c(2, 2, 2)) dimnames(a1) - list(A = c(A1, A2), B = c(B1, B2), D = c(D1, D2)) I would like to add a new dim 'group' with the value low. Right now I'm using this, but I think are better ways... a2 - as.data.frame(as.table(a1)) a2$group - low a2 - xtabs(Freq ~ A + B + D + group, data = a2) a2 Thanks for any help! Patrick __ 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, reproducible code.
Re: [R] Add a dim to an array
Hello, Try a3 - array(dim=c(2, 2, 2, 2)) dn - dimnames(a1) dn$group - c(low, high) dimnames(a3) - dn a3[] - a1 # to use [] keeps the dimensions a3 # it fills a3 recycling a1 (two copies) dim(a2) dim(a3) As you can see, 'a2' is not of the right dimensions, and 'a3' has now two copies of 'a1' but it's dimensions are right. There are ways of giving dimnames to 'a3' other than creating a new object, 'dn', but this one is a simple one. Hope this helps, Rui Barradas Em 02-06-2012 16:17, Patrick Hausmann escreveu: Dear list, I'm trying to add a new dim to a multidimensional array. My array looks like this a1 - array(1:8, c(2, 2, 2)) dimnames(a1) - list(A = c(A1, A2), B = c(B1, B2), D = c(D1, D2)) I would like to add a new dim 'group' with the value low. Right now I'm using this, but I think are better ways... a2 - as.data.frame(as.table(a1)) a2$group - low a2 - xtabs(Freq ~ A + B + D + group, data = a2) a2 Thanks for any help! Patrick __ 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, reproducible code. __ 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, reproducible code.
Re: [R] Add a dim to an array
On Sat, Jun 02, 2012 at 05:17:36PM +0200, Patrick Hausmann wrote: Dear list, I'm trying to add a new dim to a multidimensional array. My array looks like this a1 - array(1:8, c(2, 2, 2)) dimnames(a1) - list(A = c(A1, A2), B = c(B1, B2), D = c(D1, D2)) I would like to add a new dim 'group' with the value low. Right now I'm using this, but I think are better ways... a2 - as.data.frame(as.table(a1)) a2$group - low a2 - xtabs(Freq ~ A + B + D + group, data = a2) a2 Hi. Try the following. a1 - array(1:8, c(2, 2, 2)) dimnames(a1) - list(A = c(A1, A2), B = c(B1, B2), D = c(D1, D2)) a2 - array(a1, c(2, 2, 2, 1)) dimnames(a2) - c(dimnames(a1), list(group = low)) a2 , , D = D1, group = low B AB1 B2 A1 1 3 A2 2 4 , , D = D2, group = low B AB1 B2 A1 5 7 A2 6 8 Hope this helps. Petr Savicky. __ 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, reproducible code.
Re: [R] Add a dim to an array
Hello again, Sorry, I've misread your post and thought that you wanted the new dim to have 'low' and 'high' values. It's just 'low'. So dim=c(2, 2, 2, 1) and set dn$group to the first value only. And forget the final comments. Rui Barradas Em 03-06-2012 07:31, Rui Barradas escreveu: Hello, Try a3 - array(dim=c(2, 2, 2, 2)) dn - dimnames(a1) dn$group - c(low, high) dimnames(a3) - dn a3[] - a1 # to use [] keeps the dimensions a3 # it fills a3 recycling a1 (two copies) dim(a2) dim(a3) As you can see, 'a2' is not of the right dimensions, and 'a3' has now two copies of 'a1' but it's dimensions are right. There are ways of giving dimnames to 'a3' other than creating a new object, 'dn', but this one is a simple one. Hope this helps, Rui Barradas Em 02-06-2012 16:17, Patrick Hausmann escreveu: Dear list, I'm trying to add a new dim to a multidimensional array. My array looks like this a1 - array(1:8, c(2, 2, 2)) dimnames(a1) - list(A = c(A1, A2), B = c(B1, B2), D = c(D1, D2)) I would like to add a new dim 'group' with the value low. Right now I'm using this, but I think are better ways... a2 - as.data.frame(as.table(a1)) a2$group - low a2 - xtabs(Freq ~ A + B + D + group, data = a2) a2 Thanks for any help! Patrick __ 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, reproducible code. __ 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, reproducible code.