[R] Reshape or Stack? (To produce output as columns)

Chuck Cleland ccleland at optonline.net
Tue Jun 17 14:06:29 CEST 2008


On 6/17/2008 6:59 AM, Steve Murray wrote:
> Dear all,
> 
> I have used 'read.table' to create a data frame of 720 columns and 360 rows (and assigned this to 'Jan'). The row and column names are numeric:
> 
>> columnnames <- sprintf("%.2f", seq(from = -179.75, to = 179.75, length = 720)). 
>> rnames <- sprintf("%.2f", seq(from = -89.75, to = 89.75, length = 360))
>> colnames(Jan) <- columnnames
>> rownames(Jan) <- rnames
> 
> A sample of the data looks like this:
> 
>> head(Jan)
>        -179.75 -179.25 -178.75 -178.25 -177.75 -177.25 -176.75 -176.25 -175.75
> -89.75   -56.9   -64.2    56.2   -90.0    56.9   -29.0   -91.0   34.0     -9.1
> -89.25    37.9    19.3     -0.4   -12.3   -11.8   -92.1     9.2   -23.5     -0.2
> -88.75    47.4      3.1   -47.4    46.4    34.2      6.1   -41.3    44.7   -10.3
> -88.25   -20.3    34.5   -67.3   -99.9    37.9     -9.3    17.7   -17.2    63.4
> -87.75   -46.4    47.4    12.4   -48.3      9.3   -33.8    38.1    10.8   -34.1
> -87.25   -48.4    10.3   -89.3   -33.0     -1.1   -33.1    81.2    -8.3   -47.2
> 
> 
> I'm hoping to get the whole dataset into the form of columns, so that, for example, the first row (as shown above) would look like this:
> 
> Latitude   Longitude   Value
> -89.75      -179.75     -56.9
> -89.75      -179.25     -64.2
> -89.75      -178.75      56.2
> -89.75      -178.25     -90.0
> -89.75      -177.75      56.9
> -89.75      -177.25     -29.0
> -89.75      -176.75     -91.0
> -89.75      -176.25      34.0
> -89.75      -175.75      -9.1
> 
> 
> As you can see, this would require the repeated printing of the the row and column names (in this case '-89.75') - so it's not just a case of rearranging the data, but creating 'more' data too.
> 
> I've tried to achieve this using 'reshape' and 'stack' (their help files and after looking through the mailing archives), but I'm obviously doing something wrong. For reshape, I'm getting errors relating to the commands I enter, and for stack, I can only produce two columns from my data (with the additional 3rd column being a row count). In any case, these two columns refer to the wrong values (it's producing output in the form of: row count number, Longitude, Value).
> 
> I'd be very grateful if anyone could help me out with the commands I need to enter in order to achieve the results I'm hoping for.

   Here is an approach with reshape() on a much smaller example:

columnnames <- sprintf("%.2f", seq(from = -179.75, to = 179.75, length = 5))

rnames <- sprintf("%.2f", seq(from = - 89.75, to =  89.75, length = 3))

Jan <- as.data.frame(matrix(runif(3*5), ncol=5))

colnames(Jan) <- columnnames
rownames(Jan) <- rnames

Jan$Latitude <- rownames(Jan)

Jan.long <- reshape(Jan, idvar="Latitude", direction="long",
                     varying = list(columnnames),
                     v.names="Value",
                     timevar="Longitude",
                     times=columnnames)

Jan.long[] <- sapply(Jan.long, as.numeric)

Jan
          -179.75    -89.88      0.00     89.88    179.75 Latitude
-89.75 0.9264005 0.5442698 0.3894998 0.8961858 0.1340782   -89.75
0.00   0.4719097 0.1961747 0.3108708 0.1663938 0.1316141     0.00
89.75  0.1426153 0.8985805 0.1600287 0.9004246 0.1052875    89.75

Jan.long
                Latitude Longitude     Value
-89.75.-179.75   -89.75   -179.75 0.9264005
0.00.-179.75       0.00   -179.75 0.4719097
89.75.-179.75     89.75   -179.75 0.1426153
-89.75.-89.88    -89.75    -89.88 0.5442698
0.00.-89.88        0.00    -89.88 0.1961747
89.75.-89.88      89.75    -89.88 0.8985805
-89.75.0.00      -89.75      0.00 0.3894998
0.00.0.00          0.00      0.00 0.3108708
89.75.0.00        89.75      0.00 0.1600287
-89.75.89.88     -89.75     89.88 0.8961858
0.00.89.88         0.00     89.88 0.1663938
89.75.89.88       89.75     89.88 0.9004246
-89.75.179.75    -89.75    179.75 0.1340782
0.00.179.75        0.00    179.75 0.1316141
89.75.179.75      89.75    179.75 0.1052875

   You also might use expand.grid() as follows:

Jan.long2 <- cbind(expand.grid(rnames, columnnames), unlist(Jan[,1:5]))

Jan.long2[] <- sapply(Jan.long2, function(x){as.numeric(as.character(x))})

names(Jan.long2) <- c("Latitude", "Longitude", "Value")

Jan.long2
          Latitude Longitude     Value
-179.751   -89.75   -179.75 0.9264005
-179.752     0.00   -179.75 0.4719097
-179.753    89.75   -179.75 0.1426153
-89.881    -89.75    -89.88 0.5442698
-89.882      0.00    -89.88 0.1961747
-89.883     89.75    -89.88 0.8985805
0.001      -89.75      0.00 0.3894998
0.002        0.00      0.00 0.3108708
0.003       89.75      0.00 0.1600287
89.881     -89.75     89.88 0.8961858
89.882       0.00     89.88 0.1663938
89.883      89.75     89.88 0.9004246
179.751    -89.75    179.75 0.1340782
179.752      0.00    179.75 0.1316141
179.753     89.75    179.75 0.1052875

> Many thanks,
> 
> Steve
> 
> ______________________________________________
> 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. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list