Dear all! I have a data frame composed by 13 columns (year, and 12 months). I want to transform this data base in another like this year month values 1901 1 1901 2 1901 3 ..... 1901 12 1902 1 1902 2 .... 1902 12 Is there a possibility to succeed that in R? Thank you! best regards! CR -- --- Catalin-Constantin ROIBU Lecturer PhD, Forestry engineer Forestry Faculty of Suceava Str. Universitatii no. 13, Suceava, 720229, Romania office phone +4 0230 52 29 78, ext. 531 mobile phone +4 0745 53 18 01 +4 0766 71 76 58 FAX: +4 0230 52 16 64 silvic.usv.ro [[alternative HTML version deleted]]
Hello, Catalin, assume your data frame is as simple as> A <- data.frame( year = <years>, month01 = <values01>, ....,+ month12 = <values12>) then, e.g.,> reshape( A, varying = c( "month01", ...., "month12"),+ v.names = "Values", timevar = "Month", direction = "long") should do what you want. For details see ?reshape Hth -- Gerrit On Mon, 26 Aug 2013, catalin roibu wrote:> Dear all! > > I have a data frame composed by 13 columns (year, and 12 months). I want to > transform this data base in another like this > year month values > 1901 1 > 1901 2 > 1901 3 > ..... > 1901 12 > 1902 1 > 1902 2 > .... > 1902 12 > > Is there a possibility to succeed that in R? > > Thank you! > > best regards! > CR > > -- > --- > Catalin-Constantin ROIBU > Lecturer PhD, Forestry engineer > Forestry Faculty of Suceava > Str. Universitatii no. 13, Suceava, 720229, Romania > office phone +4 0230 52 29 78, ext. 531 > mobile phone +4 0745 53 18 01 > +4 0766 71 76 58 > FAX: +4 0230 52 16 64 > silvic.usv.ro > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.
On 08/26/2013 09:04 PM, catalin roibu wrote:> Dear all! > > I have a data frame composed by 13 columns (year, and 12 months). I want to > transform this data base in another like this > year month values > 1901 1 > 1901 2 > 1901 3 > ..... > 1901 12 > 1902 1 > 1902 2 > .... > 1902 12 > > Is there a possibility to succeed that in R? > > Thank you! > > best regards! > CR >Hi catalin, Try this: yrmon<-data.frame(cbind(1901:1905,matrix(rnorm(60),ncol=12))) names(yrmon)<-c("year",month.abb) library(prettyR) yrmon2<-rep_n_stack(yrmon,to.stack=2:13) yrmon2[order(yrmon2$year),] Jim
Have a look at the packages reshape and reshape2 They were written with this type of problems in mind. On Aug 26, 2013, at 1:04 PM, catalin roibu <catalinroibu at gmail.com> wrote:> Dear all! > > I have a data frame composed by 13 columns (year, and 12 months). I want to > transform this data base in another like this > year month values > 1901 1 > 1901 2 > 1901 3 > ..... > 1901 12 > 1902 1 > 1902 2 > .... > 1902 12 > > Is there a possibility to succeed that in R? > > Thank you! > > best regards! > CR > > -- > --- > Catalin-Constantin ROIBU > Lecturer PhD, Forestry engineer > Forestry Faculty of Suceava > Str. Universitatii no. 13, Suceava, 720229, Romania > office phone +4 0230 52 29 78, ext. 531 > mobile phone +4 0745 53 18 01 > +4 0766 71 76 58 > FAX: +4 0230 52 16 64 > silvic.usv.ro > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Catalin, first, keep the communication on the list, so it gets documented also for others. Second, my code does work for an example I made up myself, but if you don't "provide commented, minimal, self-contained, reproducible code" (as the posting guide asks you to; see last line of this e-mail), it is unlikely that someone could be of further help. Regards -- Gerrit On Mon, 26 Aug 2013, catalin roibu wrote:> Hello! > Your code give me the same month values for each year from interval. I want > 12 month for each year!. > Eg > year month value > 1901 1 3.5 > 1901 2 2.5 > > > On 26 August 2013 14:35, Gerrit Eichner > <Gerrit.Eichner at math.uni-giessen.de>wrote: > >> Hello, Catalin, >> >> assume your data frame is as simple as >> >> A <- data.frame( year = <years>, month01 = <values01>, ...., >>> >> + month12 = <values12>) >> >> then, e.g., >> >> reshape( A, varying = c( "month01", ...., "month12"), >>> >> + v.names = "Values", timevar = "Month", direction = "long") >> >> should do what you want. For details see ?reshape >> >> Hth -- Gerrit >> >> >> >> On Mon, 26 Aug 2013, catalin roibu wrote: >> >> Dear all! >>> >>> I have a data frame composed by 13 columns (year, and 12 months). I want >>> to >>> transform this data base in another like this >>> year month values >>> 1901 1 >>> 1901 2 >>> 1901 3 >>> ..... >>> 1901 12 >>> 1902 1 >>> 1902 2 >>> .... >>> 1902 12 >>> >>> Is there a possibility to succeed that in R? >>> >>> Thank you! >>> >>> best regards! >>> CR >>> >>> -- >>> --- >>> Catalin-Constantin ROIBU >>> Lecturer PhD, Forestry engineer >>> Forestry Faculty of Suceava >>> Str. Universitatii no. 13, Suceava, 720229, Romania >>> office phone +4 0230 52 29 78, ext. 531 >>> mobile phone +4 0745 53 18 01 >>> +4 0766 71 76 58 >>> FAX: +4 0230 52 16 64 >>> silvic.usv.ro >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________**________________ >>> R-help at r-project.org mailing list >>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >>> PLEASE do read the posting guide http://www.R-project.org/** >>> posting-guide.html <http://www.R-project.org/posting-guide.html> >>> and provide commented, minimal, self-contained, reproducible code. >>> >> > > > -- > --- > Catalin-Constantin ROIBU > Lecturer PhD, Forestry engineer > Forestry Faculty of Suceava > Str. Universitatii no. 13, Suceava, 720229, Romania > office phone +4 0230 52 29 78, ext. 531 > mobile phone +4 0745 53 18 01 > +4 0766 71 76 58 > FAX: +4 0230 52 16 64 > silvic.usv.ro