[R] reshaping pairs of columns

Phil Spector spector at stat.berkeley.edu
Wed Nov 4 00:23:50 CET 2009


Michael -
    I think this call to reshape gets you pretty close:

> reshape(maps,direction='long',
+         varying=list(grep('lat',names(dat),value=TRUE),
+                      grep('long',names(dat),value=TRUE)),
+         times=LETTERS[1:12])

     point time   latA  longA id
1.A     1    A  41.68  82.85  1
2.A     2    A  41.90  82.52  2
3.A     3    A  42.25  81.92  3
4.A     4    A  42.55  80.03  4
5.A     5    A  44.05  83.03  5
             . . .


 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu

On Tue, 3 Nov 2009, Michael Friendly wrote:

> Given a data frame consisting of a pointID and 12 pairs of (lat, long) 
> variables, with names
> latA, longA, latB, longB, ... latL, longL, I want to reshape it into a data 
> frame with the structure
>
> point  source  lat long
> 1        A       ...   ...
> 1        B      ...    ...
>
> I've looked at reshape and plyr, but can't figure out how to do this. 
> Details of my data frame are below
>
>> dput(head(maps))
> structure(list(point = 1:6, latA = c(41.68, 41.9, 42.25, 42.55,
> 44.05, 43.65), longA = c(82.85, 82.52, 81.92, 80.03, 83.03, 83.95
> ), latB = c(41.62, 42.12, 42.07, 42.47, 43.42, 43.12), longB = c(87.67,
> 87, 85.97, 84.17, 87.25, 87.92), latC = c(-41.12, 41.88, 42.13,
> 42.13, 44.23, 43.25), longC = c(-83.03, 82, 80.78, 79.55, 83.6,
> 84.48), latD = c(-41.03, 41.87, 41.98, 42, 44.13, 43.8), longD = c(-85.1,
> 85.37, 83.78, 83.18, 86.85, 87.1), latE = c(41.28, 42.08, 42.22,
> 42.07, 44.6, 43.72), longE = c(82.42, 81.17, 80.2, 80.27, 83.5,
> 83.23), latF = c(43.13, 42.35, 42.62, 42.83, 44.62, 43.62), longF = c(82.25,
> 81.77, 81.37, 79.92, 85.5, 85.25), latG = c(42.05, 42.23, 42.37,
> 42.37, 44.75, 44.03), longG = c(82.88, 82.35, 81.3, 80.03, 83.57,
> 83.47), latH = c(42.15, 42.38, 42.53, 42.87, 44.47, 44.03), longH = c(81.63,
> 80.78, 80.35, 78.73, 81.75, 81.72), latI = c(42.03, 42.25, 42.33,
> 42.33, 44.75, 44.03), longI = c(83.53, 82.45, 81.98, 80.65, 84.33,
> 84.22), latJ = c(42.02, 42.12, 42.5, 42.68, 44.35, 44.07), longJ = c(82.9,
> 82.42, 81.87, 80.15, 82.9, 83.6), latK = c(42, 42.08, 42.48,
> 42.67, 44.32, 44.03), longK = c(82.9, 82.37, 81.87, 80.18, 82.9,
> 83.57), latL = c(-41.12, 42, 42.32, 42.55, 44.27, -44.07), longL = c(-82.12,
> 82.08, 81.75, 80.08, 83, -83.12)), .Names = c("point", "latA",
> "longA", "latB", "longB", "latC", "longC", "latD", "longD", "latE",
> "longE", "latF", "longF", "latG", "longG", "latH", "longH", "latI",
> "longI", "latJ", "longJ", "latK", "longK", "latL", "longL"), row.names = 
> c(NA,
> 6L), class = "data.frame")
>
> -- 
> Michael Friendly     Email: friendly AT yorku DOT ca Professor, Psychology 
> Dept.
> York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
> 4700 Keele Street    http://www.math.yorku.ca/SCS/friendly.html
> Toronto, ONT  M3J 1P3 CANADA
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list