I have radar data in a 2D data frame with 3600 rows and 800 columns that I need to convert to a 3D array with 10 rows, 360 columns, and 800 levels. I need to split the 3600 rows into 10 groups of 360 and convert the 800 columns into the levels for the new array. So, instead of-
> dim(REF)
[1] 3600 800
I need to put the values into the form-
> dim(ref.array)
[1] 10 360 800
I know this is a pretty basic request, but I can't remember much because I haven't used R much since I took the class two years ago. Any help would be appreciated.
Edit to answer questions below.
If we take the example that @BondedDust used, we get the following-
d <- as.data.frame( matrix( 1:(3*4*5), 12, 5))
array( unlist(d), dim=c(3, 4, 5) )
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
, , 2
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
, , 3
[,1] [,2] [,3] [,4]
[1,] 25 28 31 34
[2,] 26 29 32 35
[3,] 27 30 33 36
, , 4
[,1] [,2] [,3] [,4]
[1,] 37 40 43 46
[2,] 38 41 44 47
[3,] 39 42 45 48
, , 5
[,1] [,2] [,3] [,4]
[1,] 49 52 55 58
[2,] 50 53 56 59
[3,] 51 54 57 60
What I'm looking for is an array organized like this-
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
, , 2
[,1] [,2] [,3] [,4]
[1,] 13 14 15 16
[2,] 17 18 19 20
[3,] 21 22 23 24
, , 3
[,1] [,2] [,3] [,4]
[1,] 25 26 27 28
[2,] 29 30 31 32
[3,] 33 34 35 36
, , 4
[,1] [,2] [,3] [,4]
[1,] 37 38 39 40
[2,] 41 42 43 44
[3,] 45 46 47 48
, , 5
[,1] [,2] [,3] [,4]
[1,] 49 50 51 52
[2,] 53 54 55 56
[3,] 57 58 59 60
Wouldn't this do it?
new.arr <- array( unlist(dfrm), dim=c(10, 360, 800) ) )
This is what I understood to be the task, but if it is not, then there is an aperm
function in the 'aperm' package that will allow "permuting" the dimensions:
d <- as.data.frame( matrix( 1:(3*4*5), 12, 5))
array( unlist(d), dim=c(3, 4, 5) )
##=========result============##
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
, , 2
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
, , 3
snipped rest of output
Since it is now clear that you did want the array equivalent of byrow=TRUE (which is not an option in the R array function), then this provides the desired result:
aperm( array( unlist(d), dim=c(4, 3, 5) ) , c(2,1,3) )
#===============
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
, , 2
[,1] [,2] [,3] [,4]
[1,] 13 14 15 16
[2,] 17 18 19 20
[3,] 21 22 23 24
Snipped rest of output.
I was mistaken about aperm
being in a different package.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.