[R] Reshape to wide format
David L Carlson
dcarlson at tamu.edu
Tue Dec 13 18:21:23 CET 2016
You can also use function reshape() in stats:
temp$ID<-paste(temp$iso3,temp$lon,temp$lat,sep="")
wide <- reshape(temp, v.names="precip", timevar="ID", idvar="month",
direction="wide", drop=c("iso3", "lon", "lat", "dm"))
wide
month precip.AFG6132 precip.AFG6133
1 1 0.9966658 1.133129032
2 2 0.1567117 0.355208276
3 3 0.2424774 0.307277419
4 4 0.0000000 0.008316000
5 5 0.0000000 0.000000000
6 6 0.0000000 0.000000000
7 7 0.0000000 0.000000000
8 8 0.0000000 0.000836129
9 9 0.0000000 NA
10 10 0.0000000 NA
11 11 0.1215360 NA
12 12 0.3886606 NA
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jim Lemon
Sent: Tuesday, December 13, 2016 2:59 AM
To: Miluji Sb; r-help mailing list
Subject: Re: [R] Reshape to wide format
Hi Milu,
I may have the wrong idea, but is this what you want?
temp$ID<-paste(temp$iso3,temp$lon,temp$lat,sep="")
library(prettyR)
newtemp<-stretch_df(temp,"month","precip")[,c(5,7,8)]
names(newtemp)<-c("month",unique(temp$ID))
Jim
On Tue, Dec 13, 2016 at 4:10 AM, Miluji Sb <milujisb at gmail.com> wrote:
> Dear all,
>
> I have the following monthly data by coordinates:
>
> I would like to reshape this data to wide format so that each column is a
> coordinate and each row is a month,
>
> coordinate1 coordinate2 coordinate3...
> Month 1
> Month 2
>
> Is the best option to concatenate the iso3, lon, and lat variables to
> create an ID variable? I realize that this question might be very basic but
> I'm slightly baffled. Thank you.
>
> temp <- dput(head(precip_2000,20))
> structure(list(iso3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("AFG",
> "AGO", "ALB", "ARE", "ARG", "ARM", "AUS", "AUT", "AZE", "BDI",
> "BEL", "BEN", "BFA", "BGD", "BGR", "BHS", "BIH", "BLR", "BLZ",
> "BOL", "BRA", "BRN", "BTN", "BWA", "CAF", "CAN", "CHE", "CHL",
> "CHN", "CIV", "CMR", "COD", "COG", "COL", "CRI", "CUB", "CYP",
> "CZE", "DEU", "DJI", "DNK", "DOM", "DZA", "ECU", "EGY", "ERI",
> "ESH", "ESP", "EST", "ETH", "FIN", "FJI", "FLK", "FRA", "GAB",
> "GBR", "GEO", "GHA", "GIN", "GNB", "GNQ", "GRC", "GRL", "GTM",
> "GUF", "GUY", "HND", "HRV", "HTI", "HUN", "IDN", "IND", "IRL",
> "IRN", "IRQ", "ISL", "ISR", "ITA", "JAM", "JOR", "JPN", "KAZ",
> "KEN", "KGZ", "KHM", "KIR", "KOR", "KWT", "LAO", "LBN", "LBR",
> "LBY", "LCA", "LKA", "LSO", "LTU", "LUX", "LVA", "MAR", "MDA",
> "MDG", "MEX", "MKD", "MLI", "MMR", "MNE", "MNG", "MOZ", "MRT",
> "MWI", "MYS", "NAM", "NCL", "NER", "NGA", "NIC", "NLD", "NOR",
> "NPL", "NZL", "OMN", "PAK", "PAN", "PER", "PHL", "PNG", "POL",
> "PRI", "PRK", "PRT", "PRY", "QAT", "ROU", "RUS", "RWA", "SAU",
> "SDN", "SEN", "SJM", "SLB", "SLE", "SLV", "SOM", "SRB", "SUR",
> "SVK", "SVN", "SWE", "SWZ", "SYR", "TCD", "TGO", "THA", "TJK",
> "TKM", "TLS", "TUN", "TUR", "TWN", "TZA", "UGA", "UKR", "URY",
> "USA", "UZB", "VEN", "VNM", "VUT", "YEM", "ZAF", "ZMB", "ZWE"
> ), class = "factor"), lon = c(61L, 61L, 61L, 61L, 61L, 61L, 61L,
> 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L
> ), lat = c(32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L,
> 32L, 32L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L), dm = structure(c(1L,
> 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 2L, 3L, 4L, 1L, 5L, 6L, 7L,
> 8L, 9L, 10L, 11L), .Label = c("2000m1", "2000m10", "2000m11",
> "2000m12", "2000m2", "2000m3", "2000m4", "2000m5", "2000m6",
> "2000m7", "2000m8", "2000m9"), class = "factor"), month = c(1L,
> 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L,
> 5L, 6L, 7L, 8L), precip = c(0.996665806451613, 0.156711724137931,
> 0.242477419354839, 0, 0, 0, 0, 0, 0, 0, 0.121536, 0.38866064516129,
> 1.13312903225806, 0.355208275862069, 0.307277419354839, 0.008316,
> 0, 0, 0, 0.0008361290322581)), .Names = c("iso3", "lon", "lat",
> "dm", "month", "precip"), row.names = c(NA, 20L), class = "data.frame")
>
> Sincerely,
>
> Milu
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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