Hello,
One of those would be with package reshape2.
dta <- read.csv( "http://doylesdartden.com/R/ExampleData.csv")
subdta <- dta[, c("Location", "Year",
"GW_Elevation")]
res <- reshape2::dcast(subdta, Location ~ Year, value.var =
"GW_Elevation")
names(res)[-1] <- paste("GW_Elevation", names(res)[-1], sep =
"_")
head(res)
Hope this helps,
Rui Barradas
On 20/08/2018 21:37, Rui Barradas wrote:> Hello,
>
> This is a very frequent question.
> I could rewrite one or two answers taken from StackOverflow:
>
>
https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format
>
>
>
> But there you will have more options.
>
>
> Hope this helps,
>
> Rui Barradas
>
> On 20/08/2018 20:17, David Doyle wrote:
>> Hello everyone,
>>
>> I'm trying to generate tables of my data out of R for my report.
>>
>> My data is setup in the format as follows and the example can be found
>> at:
>> http://doylesdartden.com/R/ExampleData.csv
>>
>> Location??????? Date??????? Year????? GW_Elevation
>> 127(I)??????? 5/14/2006???? 2006?????? 752.46
>> 119(I)??????? 5/14/2006???? 2006?????? 774.67
>> 127(I)??????? 6/11/2007???? 2007?????? 752.06
>> 119(I)??????? 6/11/2007???? 2007?????? 775.57
>>
>> I would like to generate a table that showed
>>
>> Location??? GW_Elevation 2006??? GW_Elevation 2007??? GW_Elevation
>> xxx.....
>>
>> 119(I)??????????????????? 774.67????????????????????? 775.57
>> ?????????? xxxx
>> 127(I)??????????????????? 752.46????????????????????? 752.06
>> ?????????? xxxx
>> XXXX????????????????????????? XX?????????????????????????? XX
>>
>> ? Any thoughts on how to transform the data so it would be in this
>> format??
>>
>> Thank you for your time
>>
>> David Doyle
>>
>> ????[[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.
>>
>
> ---
> This email has been checked for viruses by AVG.
> https://www.avg.com
>
> ______________________________________________
> 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.
Sorry, there is no need to subset the data frame, reshape2::dcast(dta, etc) will do the same. Rui Barradas On 21/08/2018 05:10, Rui Barradas wrote:> Hello, > > One of those would be with package reshape2. > > > > dta <- read.csv( "http://doylesdartden.com/R/ExampleData.csv") > > subdta <- dta[, c("Location", "Year", "GW_Elevation")] > > res <- reshape2::dcast(subdta, Location ~ Year, value.var = "GW_Elevation") > names(res)[-1] <- paste("GW_Elevation", names(res)[-1], sep = "_") > head(res) > > > Hope this helps, > > Rui Barradas > > On 20/08/2018 21:37, Rui Barradas wrote: >> Hello, >> >> This is a very frequent question. >> I could rewrite one or two answers taken from StackOverflow: >> >> https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format >> >> >> >> But there you will have more options. >> >> >> Hope this helps, >> >> Rui Barradas >> >> On 20/08/2018 20:17, David Doyle wrote: >>> Hello everyone, >>> >>> I'm trying to generate tables of my data out of R for my report. >>> >>> My data is setup in the format as follows and the example can be >>> found at: >>> http://doylesdartden.com/R/ExampleData.csv >>> >>> Location??????? Date??????? Year????? GW_Elevation >>> 127(I)??????? 5/14/2006???? 2006?????? 752.46 >>> 119(I)??????? 5/14/2006???? 2006?????? 774.67 >>> 127(I)??????? 6/11/2007???? 2007?????? 752.06 >>> 119(I)??????? 6/11/2007???? 2007?????? 775.57 >>> >>> I would like to generate a table that showed >>> >>> Location??? GW_Elevation 2006??? GW_Elevation 2007??? GW_Elevation >>> xxx..... >>> >>> 119(I)??????????????????? 774.67????????????????????? 775.57 >>> ?????????? xxxx >>> 127(I)??????????????????? 752.46????????????????????? 752.06 >>> ?????????? xxxx >>> XXXX????????????????????????? XX?????????????????????????? XX >>> >>> ? Any thoughts on how to transform the data so it would be in this >>> format?? >>> >>> Thank you for your time >>> >>> David Doyle >>> >>> ????[[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. >>> >> >> --- >> This email has been checked for viruses by AVG. >> https://www.avg.com >> >> ______________________________________________ >> 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.
Another approach to adding GW_Elevation to the year value, but the table is more
compact with just the year.
dta <- read.csv("http://doylesdartden.com/R/ExampleData.csv")
Years <- paste("GW_Elevation", dta$Year)
xtabs(GW_Elevation~Location+Years, dta)
David L. Carlson
Department of Anthropology
Texas A&M University
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Rui Barradas
Sent: Monday, August 20, 2018 11:39 PM
To: David Doyle <kydaviddoyle at gmail.com>; r-help at r-project.org
Subject: Re: [R] Transforming data for nice output table
Sorry, there is no need to subset the data frame,
reshape2::dcast(dta, etc)
will do the same.
Rui Barradas
On 21/08/2018 05:10, Rui Barradas wrote:> Hello,
>
> One of those would be with package reshape2.
>
>
>
> dta <- read.csv( "http://doylesdartden.com/R/ExampleData.csv")
>
> subdta <- dta[, c("Location", "Year",
"GW_Elevation")]
>
> res <- reshape2::dcast(subdta, Location ~ Year, value.var =
"GW_Elevation")
> names(res)[-1] <- paste("GW_Elevation", names(res)[-1], sep =
"_")
> head(res)
>
>
> Hope this helps,
>
> Rui Barradas
>
> On 20/08/2018 21:37, Rui Barradas wrote:
>> Hello,
>>
>> This is a very frequent question.
>> I could rewrite one or two answers taken from StackOverflow:
>>
>>
https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format
>>
>>
>>
>> But there you will have more options.
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> On 20/08/2018 20:17, David Doyle wrote:
>>> Hello everyone,
>>>
>>> I'm trying to generate tables of my data out of R for my
report.
>>>
>>> My data is setup in the format as follows and the example can be
>>> found at:
>>> http://doylesdartden.com/R/ExampleData.csv
>>>
>>> Location??????? Date??????? Year????? GW_Elevation
>>> 127(I)??????? 5/14/2006???? 2006?????? 752.46
>>> 119(I)??????? 5/14/2006???? 2006?????? 774.67
>>> 127(I)??????? 6/11/2007???? 2007?????? 752.06
>>> 119(I)??????? 6/11/2007???? 2007?????? 775.57
>>>
>>> I would like to generate a table that showed
>>>
>>> Location??? GW_Elevation 2006??? GW_Elevation 2007??? GW_Elevation
>>> xxx.....
>>>
>>> 119(I)??????????????????? 774.67????????????????????? 775.57
>>> ?????????? xxxx
>>> 127(I)??????????????????? 752.46????????????????????? 752.06
>>> ?????????? xxxx
>>> XXXX????????????????????????? XX?????????????????????????? XX
>>>
>>> ? Any thoughts on how to transform the data so it would be in this
>>> format??
>>>
>>> Thank you for your time
>>>
>>> David Doyle
>>>
>>> ????[[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.
>>>
>>
>> ---
>> This email has been checked for viruses by AVG.
>> https://www.avg.com
>>
>> ______________________________________________
>> 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.
______________________________________________
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.