Your original data does not contain a field called year. This will not give you what you asked for which was a separate row for each month and you have specified month as part of the timevar (columns) and the idvar (rows). Perhaps you want year and month to specify the rows using idvar=c("year", "month")?
----------------------------------------------- David L. Carlson Department of Anthropology Texas A&M University From: Miluji Sb [mailto:miluj...@gmail.com] Sent: Thursday, December 22, 2016 8:19 AM To: David L Carlson <dcarl...@tamu.edu> Cc: Jim Lemon <drjimle...@gmail.com>; r-help mailing list <r-help@r-project.org> Subject: Re: [R] Reshape to wide format Apologies for the late reply. Thank you very much! I get the following warnings. If I modify the code to add both month and year as part of the ID, will it still be correct? df$ID<-paste(df$iso3,df$lon,df$lat,df$year,df$month,sep="") wide <- reshape(df, v.names="precip", timevar="ID", idvar="month", direction="wide", drop=c("iso3", "lon", "lat", "year")) Sincerely, Milu warnings() Warning messages: 1: In reshapeWide(data, idvar = idvar, timevar = timevar, ... : some constant variables (year) are really varying 2: In reshapeWide(data, idvar = idvar, timevar = timevar, ... : multiple rows match for ID=AFG6132: first taken 3: In reshapeWide(data, idvar = idvar, timevar = timevar, ... : multiple rows match for ID=AFG6133: first taken 4: In reshapeWide(data, idvar = idvar, timevar = timevar, ... : multiple rows match for ID=AFG6134: first taken On Tue, Dec 13, 2016 at 6:21 PM, David L Carlson <dcarl...@tamu.edu> wrote: 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-boun...@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 <miluj...@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@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@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@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.