From chaceh at terpmail.umd.edu Fri Sep 9 18:56:13 2016 From: chaceh at terpmail.umd.edu (chaceh) Date: Fri, 9 Sep 2016 09:56:13 -0700 (PDT) Subject: [datatable-help] question on wide to long conversion Message-ID: <1473440173217-4724572.post@n4.nabble.com> I have a data table that I'd like to convert from wide to long. I know of 2 good ways to do this, either with melt() or reshape(). The problem is, my first row (i.e. column names) are not easy to manipulate. here's a screen shot of the data: What I want to do, is create a column for the lake, year, ph, and SO4, so it would look something like this: Lake year pH SO4 1 1976 4.59 6.5 1 1977 -9999 -9999 1 1978 4.48 7.3 1 1981 4.63 6 2 1976 4.97 5.5 and so on because the column names are not the year, I'm not exactly sure how to melt or reshape it to fit what I want. any suggestions? Thanks a lot for your help -- View this message in context: http://r.789695.n4.nabble.com/question-on-wide-to-long-conversion-tp4724572.html Sent from the datatable-help mailing list archive at Nabble.com. From mel at mbacou.com Fri Sep 9 23:20:36 2016 From: mel at mbacou.com (Bacou, Melanie) Date: Fri, 9 Sep 2016 17:20:36 -0400 Subject: [datatable-help] question on wide to long conversion In-Reply-To: <1473440173217-4724572.post@n4.nabble.com> References: <1473440173217-4724572.post@n4.nabble.com> Message-ID: <2b76e4f8-2bc3-9715-d8a6-eb63e150fc94@mbacou.com> You will need to first combine the first 2 rows into a single header row with unique header names before you can melt it, e.g. require(stringr) dt <- fread("path to your csv file", skip=1) setnames(dt, 4:11, c(paste0("pH", 1976:1981), paste0("SO4", 1976:1981))) dt <- melt(dt, id.vars=1:3, variable.name="year") dt[, variable := str_sub(year, 1, -5)] dt[, year := str_sub(year, -4, -1)] dt <- dcast(dt, Lake+year~variable) --Mel. On 9/9/2016 12:56 PM, chaceh wrote: > I have a data table that I'd like to convert from wide to long. I know of 2 > good ways to do this, either with melt() or reshape(). The problem is, my > first row (i.e. column names) are not easy to manipulate. here's a screen > shot of the data: > > > > What I want to do, is create a column for the lake, year, ph, and SO4, so it > would look something like this: > > Lake year pH SO4 > 1 1976 4.59 6.5 > 1 1977 -9999 -9999 > 1 1978 4.48 7.3 > 1 1981 4.63 6 > 2 1976 4.97 5.5 > and so on > > because the column names are not the year, I'm not exactly sure how to melt > or reshape it to fit what I want. any suggestions? Thanks a lot for your > help > > > > -- > View this message in context: http://r.789695.n4.nabble.com/question-on-wide-to-long-conversion-tp4724572.html > Sent from the datatable-help mailing list archive at Nabble.com. > _______________________________________________ > datatable-help mailing list > datatable-help at lists.r-forge.r-project.org > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help From hjhamilton4 at gmail.com Thu Sep 22 23:15:02 2016 From: hjhamilton4 at gmail.com (hamilthj) Date: Thu, 22 Sep 2016 14:15:02 -0700 (PDT) Subject: [datatable-help] Fitting a line to a log-log graph Message-ID: <1474578902535-4725006.post@n4.nabble.com> I am generating a simple body length-weight regression plot where the variables form a logistic relationship on normal axes. To simplify the graph and have the true variables on either axes, I've log transformed the axes and the now linear relationship between the variables is obvious. I am having an impossible time trying to fit a line to this graph. plot(Average.BL~Wet.weight, log="xy") reg=lm(log(Average.BL)~log(Wet.weight)) reg #used intercepts from output abline(log(2.9978), 0.3289, untf=F) Here is the graph that's generated: Not sure where to go from here, the slope looks right but obviously the intercept is offset and I'm not sure why. -- View this message in context: http://r.789695.n4.nabble.com/Fitting-a-line-to-a-log-log-graph-tp4725006.html Sent from the datatable-help mailing list archive at Nabble.com.