[R] Convert 2-dim array to 3-dim array

David Winsemius dwinsemius at comcast.net
Mon Jun 4 16:45:17 CEST 2012


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



More information about the R-help mailing list