[R] resizing data

David Winsemius dwinsemius at comcast.net
Fri Jan 25 22:54:16 CET 2013


On Jan 25, 2013, at 1:12 PM, emorway wrote:

> Undoubtedly this question has been asked before, I just can't seem to find
> the combination of search terms to produce it.  I'm trying to resize a
> dataset that is pulled into R using read.table.  However, I think the same
> problem can be produced using matrix:
> 
> x<-matrix(1:64,8)
> x
> #     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> #[1,]    1    9   17   25   33   41   49   57
> #[2,]    2   10   18   26   34   42   50   58
> #[3,]    3   11   19   27   35   43   51   59
> 
snipped
> 
> The true order of data in the larger problem I'm working with is actually
> transposed, like so:
> x<-t(x)
> x
> #     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> #[1,]    1    2    3    4    5    6    7    8
> #[2,]    9   10   11   12   13   14   15   16
> #[3,]   17   18   19   20   21   22   23   24
> 
...
snipped
> I'm trying to resize the data (in this example, a matrix) to say a 16 x 4
> matrix while preserving the consecutive order of the individual elements in
> a left-to-right top-to-bottom fashion.  The example below is wrong because
> the first row should be "1 2 3 4", how can I make this happen?  It would
> also be nice to make a 4 x 16 matrix where the first row contains the values
> of x[1,1:8] followed by x[2,1:8].  I'm guessing there is a 1 liner of R code
> for this type of thing so I don't have to resort to nested for loops?
> 
> y<-matrix(x,nrow=16,ncol=4)
> y
> #      [,1] [,2] [,3] [,4]
> # [1,]    1    3    5    7
> # [2,]    9   11   13   15
> # [3,]   17   19   21   23
> ...
snipped
> 
> What's wrong with:


X1.4 <-  t( matrix(x, nrow=4) )

-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list