Question

I am working on data analysis and trying to write the simplest code for now.

setwd("/Users/Gizmo/Desktop/Harry")
harry_data <- dir()

HarryData <- array(0,dim=c(4464,8,15))

h13 <- read.table(harry_data[1],skip=2,header=FALSE)

HarryData[,,1] <- matrix(h13,4464,8)

I checked dimensions for HarryData, h13, and matrix and they are all in same dimension. But if I put data, h13 in HarryData, it doesn't work.

I get this error Error in HarryData[, , 1] : incorrect number of dimensions if HarryData[,,1]on Console

Part of My data in h13 is like this obtained from ftp://amrc.ssec.wisc.edu/pub/aws/10min/rdr/2013/089000113.r:

     V1  V2    V3    V4    V5  V6    V7    V8
1     1   1  -7.8 879.0   5.6 360 444.0   9.1
2     1   2  -7.9 879.1   4.6 360 444.0   9.1
3     1   3  -7.6 879.2   4.1 345 444.0   9.1
4     1   4  -7.6 879.3   4.1 339 444.0   9.1
5     1   5  -7.6 879.4   4.8 340 444.0   9.1
6     1   6  -7.9 879.4   3.6 340 444.0   9.1
7     1   7  -8.0 879.3   4.6 340 444.0   9.1
8     1   8  -8.0 879.4   4.1 340 444.0   9.1
9     1   9  -8.2 879.4   5.8 338 444.0   9.1
10    1  10  -8.4 879.5   4.6 339 444.0   9.1
11    1  11  -8.4 879.5   3.8 350 444.0   9.1
12    1  12  -8.1 879.5   4.1 347 444.0   9.1
13    1  13  -8.4 879.6   4.1 347 444.0   9.1
14    1  14  -8.0 879.6   5.1 357 444.0   9.1
15    1  15  -8.4 879.5   4.8 342 444.0   9.1
16    1  16  -8.5 879.7   4.1 359 444.0   9.1
17    1  17  -8.8 879.6   4.3 338 444.0   9.1
18    1  18  -8.4 879.7   3.6 350 444.0   9.1
19    1  19  -8.2 879.8   5.3 346 444.0   9.1
20    1  20  -7.9 879.8   1.3 360 444.0   9.1

What can be the problem causing this?

Was it helpful?

Solution

you should use as.matrix instead of matrix:

HarryData[,,1] <- as.matrix(h13)

OTHER TIPS

HarryData[,,1] is a vector and matrix(h13,4464,8) is a matrix. Check length(HarryData[,,1]) and dim(matrix(h13,4464,8)).

This should work:

HarryData[,,1] <- h13

Or this:

HarryData[,,1] <- as.matrix(h13)

Or even this:

HarryData[,,1] <- as.matrix(h13, 4464, 8)

Look at the output of matrix(h13, 4464, 8) to see why it doesn't work. Basically, what happens is that matrix will recycle each element in h13 where each element is a column instead of a value until it gets 4464 * 8 elements (so basically generates 4464 copies of each column), and then stores each of those columns in a cell of a 4464 * 8 "matrix", which is really a 2D list, not a real matrix. This happens because matrix is treating h13 as a list, not a data frame. When you try to insert this "matrix" like list into a matrix size slot in HarryData, R freaks out.

This is a weird corner case of the logic inside matrix (not saying that the logic of matrix is unreasonable, just that odd things happen).

For a simpler illustration of what's going on, consider:

matrix(list(1, list(1, 2, 3), "hello"), ncol=3, nrow=3)

which produces:

     [,1]    [,2]    [,3]   
[1,] 1       1       1      
[2,] List,3  List,3  List,3 
[3,] "hello" "hello" "hello"

Not a matrix in the typical sense. Notice how our three element list was recycled to fill our "matrix".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top