On 10/22/2016 10:52 AM, lily li wrote:
The code I'm using is:
require(reshape)
DF2 = melt(DF, id.vars = c('year', 'month', 'day'), measure.vars =
c('site1_elev', 'site2_elev', 'site1_temp', 'site2_temp'))
But it didn't work.
On Sat, Oct 22, 2016 at 11:50 AM, lily li <chocol...@gmail.com> wrote:
Hi R users,
I want to melt a dataframe, but it mixed up the variables.
DF is the original dataset:
year month day site1_elev site2_elev site1_temp site2_temp
2000 5 6 1300 1500 20
21
2000 5 7 1300 1500 21 22
2000 5 8 1300 1500 19 20
2000 5 9 1300 1500 22 23
How to melt the dataframe and get the following dataset? Thanks for your
help.
year month day siteID elev temp
2000 5 6 1 1300 20
2000 5 6 2 1500 21
2000 5 7 1 1300 21
2000 5 7 2 1500 22
[[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.
I am sure someone will come along with a "better" answer, but I played
with this a bit and this is what I came up with. After running your code
I ran
DF2$site <- substr(as.character(DF2$variable),1,5)
DF2$var <- substr(as.character(DF2$variable),7,10)
DF3 <- cast(DF2,year + month + day + site ~ var )
Hope this is helpful,
Dan
--
Daniel Nordlund
Port Townsend, WA USA
______________________________________________
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.