[R] Reshape to wide format

Subodh Adhikari subodh.adhikari1 at gmail.com
Tue Dec 13 01:27:28 CET 2016


For long to wide, try *dcas*t function in reshape2 package. (*melt* is from
wide to long).
Subodh

On Mon, Dec 12, 2016 at 10:10 AM, Miluji Sb <milujisb at gmail.com> wrote:

> [image: Boxbe] <https://www.boxbe.com/overview> Miluji Sb (
> milujisb at gmail.com) is not on your Guest List
> <https://www.boxbe.com/approved-list?tc_serial=27970590433&tc_rand=207238374&utm_source=stf&utm_medium=email&utm_campaign=ANNO_MWTP&utm_content=001&key=Rie9cz5zwnfjZUTkAyjbuHbUZyg4xi9ABP3GXbPk3wE%3D&token=0PSll570AP37QuCLt5N8K4YQYhleAA9pNQcUSXeBvi4ZnzoK6P%2BsykOpLi08Uza2>
> | Approve sender
> <https://www.boxbe.com/anno?tc_serial=27970590433&tc_rand=207238374&utm_source=stf&utm_medium=email&utm_campaign=ANNO_MWTP&utm_content=001&key=Rie9cz5zwnfjZUTkAyjbuHbUZyg4xi9ABP3GXbPk3wE%3D&token=0PSll570AP37QuCLt5N8K4YQYhleAA9pNQcUSXeBvi4ZnzoK6P%2BsykOpLi08Uza2>
> | Approve domain
> <https://www.boxbe.com/anno?tc_serial=27970590433&tc_rand=207238374&utm_source=stf&utm_medium=email&utm_campaign=ANNO_MWTP&utm_content=001&dom&key=Rie9cz5zwnfjZUTkAyjbuHbUZyg4xi9ABP3GXbPk3wE%3D&token=0PSll570AP37QuCLt5N8K4YQYhleAA9pNQcUSXeBvi4ZnzoK6P%2BsykOpLi08Uza2>
>
> 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.
>
>


-- 
*Subodh Adhikari*

*सुबोध अधिकारी *
PhD Candidate
Land Resources and Environmental Sciences,
Montana State University, USA

*"The Weak can Never Forgive. Forgiveness is the Attribute of the Strong" -*
* Gandhi*
*"Injustice Anywhere is a Threat to Justice Everywhere"*
*- Martin Luther King, Jr. *

	[[alternative HTML version deleted]]



More information about the R-help mailing list