[R] Convert 2-dim array to 3-dim array
David Winsemius
dwinsemius at comcast.net
Mon Jun 4 20:11:49 CEST 2012
On Jun 4, 2012, at 12:54 PM, arun wrote:
> Hi Dave,
>
> I tested the "as.array". It gives a different dimension when
> compared to "array"
Interesting. `as.array basically throws away a 'dim' argument and
substitutes length(x). Thanks for the correction.
--
David.
>
> set.seed(1)
> datE<-data.frame(A=rnorm(30,0.5))
> myArr=as.array(unlist(datE),dim=c(5,2,3))
>> myArr
> A1 A2 A3 A4 A5
> A6
> -0.12645381 0.68364332 -0.33562861 2.09528080 0.82950777
> -0.32046838
> A7 A8 A9 A10 A11
> A12
> 0.98742905 1.23832471 1.07578135 0.19461161 2.01178117
> 0.88984324
> A13 A14 A15 A16 A17
> A18
> -0.12124058 -1.71469989 1.62493092 0.45506639 0.48380974
> 1.44383621
> A19 A20 A21 A22 A23
> A24
> 1.32122120 1.09390132 1.41897737 1.28213630 0.57456498
> -1.48935170
> A25 A26 A27 A28 A29
> A30
> 1.11982575 0.44387126 0.34420449 -0.97075238 0.02184994
> 0.91794156
>> is.array(myArr)
> [1] TRUE
>> dim(myArr)
> [1] 30
>
>
>> myArr1=array(unlist(datE),dim=c(5,2,3))
>> myArr1
> , , 1
>
> [,1] [,2]
> [1,] -0.1264538 -0.3204684
> [2,] 0.6836433 0.9874291
> [3,] -0.3356286 1.2383247
> [4,] 2.0952808 1.0757814
> [5,] 0.8295078 0.1946116
>
> , , 2
>
> [,1] [,2]
> [1,] 2.0117812 0.4550664
> [2,] 0.8898432 0.4838097
> [3,] -0.1212406 1.4438362
> [4,] -1.7146999 1.3212212
> [5,] 1.6249309 1.0939013
>
> , , 3
>
> [,1] [,2]
> [1,] 1.418977 0.44387126
> [2,] 1.282136 0.34420449
> [3,] 0.574565 -0.97075238
> [4,] -1.489352 0.02184994
> [5,] 1.119826 0.91794156
>
>> dim(myArr1)
> [1] 5 2 3
>
>
> A.K.
>
>
>
>
> ----- Original Message -----
> From: David Winsemius <dwinsemius at comcast.net>
> To: David Zastrau <davidz at tzi.de>
> Cc: r-help at r-project.org
> Sent: Monday, June 4, 2012 10:45 AM
> Subject: Re: [R] Convert 2-dim array to 3-dim array
>
>
> On Jun 4, 2012, at 5:08 AM, David Zastrau wrote:
>
>> Hello R-users,
>>
>> I'd like to load data from a CSV-file to a 3-dimensional array.
>> However
>> the default seems to be 2-dimensional.
>
> No. that is not the problem.
>> It would also be ok to load the
>> data and then convert it to a 3-dimensional structure.
>>
>> I've been trying:
>>
>> dat = read.csv(filename)
>> myArr = as.array(dat, dim = c(12,100,3))
>>
>> However, I get an error message:
>>
>> Error in `dimnames<-.data.frame`(`*tmp*`, value = list(n)) :
>> invalid 'dimnames' given for data frame
>
> A dataframe is a list structure while both array and matrix are
> expecting the first argument to be an atomic vector. Try this
> (although it is a blind guess because you have not provided the
> structure of 'dat'
>
> myArr = as.array( unlist(dat), dim = c(12,100,3))
>
> --David.
>
>>
>>
>> And I don't know how how to set the dimnames.
>>
>> I would appreciate any help!
>>
>> Best Wishes
>> Dave
>>
>> [[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.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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.
>
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list