Joel Maxuel
2018-Oct-22 23:55 UTC
[R] Transformations in Tidyverse (renaming and ordering columns)
For clarity sake. More show (with example closer to reality), less tell. :^) ## Current:> library(knitr) > library(tidyverse)?? Conflicts ????????????????????????????????????????????????????????????????????? tidyverse_conflicts() ?? x dplyr::filter() masks stats::filter() x dplyr::lag() masks stats::lag()> library(tibble) > library(dplyr) > > testset <- as_tibble(tribble(~SN, ~Section, ~Order, ~Observation, ~Seq,~Label, ~Value, + 2, "For Reporting Quarter", 1, "One", 1, "Western", 163, + 2, "For Reporting Quarter", 1, "One", 2, "Northern", 105, + 2, "For Reporting Quarter", 1, "One", 3, "Eastern", 121, + 2, "For Reporting Quarter", 1, "One", 4, "Southern", 74, + 2, "For Reporting Quarter", 2, "Two", 1, "Western", 147, + 2, "For Reporting Quarter", 2, "Two", 2, "Northern", 100, + 2, "For Reporting Quarter", 2, "Two", 3, "Eastern", 106, + 2, "For Reporting Quarter", 2, "Two", 4, "Southern", 70, + 2, "For Reporting Quarter", 3, "Three", 1, "Western", 119, + 2, "For Reporting Quarter", 3, "Three", 2, "Northern", 82, + 2, "For Reporting Quarter", 3, "Three", 3, "Eastern", 90, + 2, "For Reporting Quarter", 3, "Three", 4, "Southern", 65))> testset %>% select(Observation, Label, Value) %>% spread(key=Observation,value=Value) # A tibble: 4 x 4 Label One Three Two <chr> <dbl> <dbl> <dbl> 1 Eastern 121 90 106 2 Northern 105 82 100 3 Southern 74 65 70 4 Western 163 119 147>## Intended: # A tibble: 4 x 4 For Reporting Quarter One Two Three <chr> <dbl> <dbl> <dbl> 1 Western 163 147 119 2 Northern 105 100 82 3 Eastern 121 106 90 4 Southern 74 70 65>## Unfortunately I don't know how to get there from here. Section, Order and Seq are there to assist with getting the data to the right output programmatically, however I don't know how to make use of them. Hope this helps. -- Cheers, Joel Maxuel On Mon, Oct 22, 2018 at 6:18 PM Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:> If you are willing to work in the context of LaTeX output then perhaps you > will find the "tables" package useful. However, while you think you have > communicated clearly enough regarding what you want to accomplish, I do > not, so either someone else will intuit what you want or you will create a > mock-up of what you want your output to look like to remove the guesswork. > >[[alternative HTML version deleted]]
Bert Gunter
2018-Oct-23 01:31 UTC
[R] Transformations in Tidyverse (renaming and ordering columns)
For clarity's sake: ** Stop posting in HTML.** Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Oct 22, 2018 at 4:55 PM Joel Maxuel <j.maxuel at gmail.com> wrote:> For clarity sake. More show (with example closer to reality), less tell. > :^) > > ## Current: > > > library(knitr) > > library(tidyverse) > ?? Conflicts > ????????????????????????????????????????????????????????????????????? > tidyverse_conflicts() ?? > x dplyr::filter() masks stats::filter() > x dplyr::lag() masks stats::lag() > > library(tibble) > > library(dplyr) > > > > testset <- as_tibble(tribble(~SN, ~Section, ~Order, ~Observation, ~Seq, > ~Label, ~Value, > + 2, "For Reporting Quarter", 1, "One", 1, > "Western", 163, > + 2, "For Reporting Quarter", 1, "One", 2, > "Northern", 105, > + 2, "For Reporting Quarter", 1, "One", 3, > "Eastern", 121, > + 2, "For Reporting Quarter", 1, "One", 4, > "Southern", 74, > + 2, "For Reporting Quarter", 2, "Two", 1, > "Western", 147, > + 2, "For Reporting Quarter", 2, "Two", 2, > "Northern", 100, > + 2, "For Reporting Quarter", 2, "Two", 3, > "Eastern", 106, > + 2, "For Reporting Quarter", 2, "Two", 4, > "Southern", 70, > + 2, "For Reporting Quarter", 3, "Three", 1, > "Western", 119, > + 2, "For Reporting Quarter", 3, "Three", 2, > "Northern", 82, > + 2, "For Reporting Quarter", 3, "Three", 3, > "Eastern", 90, > + 2, "For Reporting Quarter", 3, "Three", 4, > "Southern", 65)) > > testset %>% select(Observation, Label, Value) %>% spread(key=Observation, > value=Value) > # A tibble: 4 x 4 > Label One Three Two > <chr> <dbl> <dbl> <dbl> > 1 Eastern 121 90 106 > 2 Northern 105 82 100 > 3 Southern 74 65 70 > 4 Western 163 119 147 > > > > ## Intended: > > # A tibble: 4 x 4 > For Reporting Quarter One Two Three > <chr> <dbl> <dbl> <dbl> > 1 Western 163 147 119 > 2 Northern 105 100 82 > 3 Eastern 121 106 90 > 4 Southern 74 70 65 > > > > ## > > Unfortunately I don't know how to get there from here. Section, Order and > Seq are there to assist with getting the data to the right output > programmatically, however I don't know how to make use of them. > > Hope this helps. > > -- > Cheers, > Joel Maxuel > > > On Mon, Oct 22, 2018 at 6:18 PM Jeff Newmiller <jdnewmil at dcn.davis.ca.us> > wrote: > > > If you are willing to work in the context of LaTeX output then perhaps > you > > will find the "tables" package useful. However, while you think you have > > communicated clearly enough regarding what you want to accomplish, I do > > not, so either someone else will intuit what you want or you will create > a > > mock-up of what you want your output to look like to remove the > guesswork. > > > > > > [[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. >[[alternative HTML version deleted]]
John Laing
2018-Oct-23 01:47 UTC
[R] Transformations in Tidyverse (renaming and ordering columns)
I don't know much about the Tidyverse, but more generally the way to represent ordered categorical data is with a factor. This seems to work:> testset$Observation <- factor(testset$Observation, levels=c("One", "Two","Three"))> testset$Label <- factor(testset$Label, levels=c("Western", "Northern","Eastern", "Southern"))> testset %>% select(Observation, Label, Value) %>% spread(key=Observation,value=Value) # A tibble: 4 x 4 Label One Two Three <fct> <dbl> <dbl> <dbl> 1 Western 163 147 119 2 Northern 105 100 82 3 Eastern 121 106 90 4 Southern 74 70 65 JL On Mon, Oct 22, 2018 at 7:55 PM Joel Maxuel <j.maxuel at gmail.com> wrote:> For clarity sake. More show (with example closer to reality), less tell. > :^) > > ## Current: > > > library(knitr) > > library(tidyverse) > ?? Conflicts > ????????????????????????????????????????????????????????????????????? > tidyverse_conflicts() ?? > x dplyr::filter() masks stats::filter() > x dplyr::lag() masks stats::lag() > > library(tibble) > > library(dplyr) > > > > testset <- as_tibble(tribble(~SN, ~Section, ~Order, ~Observation, ~Seq, > ~Label, ~Value, > + 2, "For Reporting Quarter", 1, "One", 1, > "Western", 163, > + 2, "For Reporting Quarter", 1, "One", 2, > "Northern", 105, > + 2, "For Reporting Quarter", 1, "One", 3, > "Eastern", 121, > + 2, "For Reporting Quarter", 1, "One", 4, > "Southern", 74, > + 2, "For Reporting Quarter", 2, "Two", 1, > "Western", 147, > + 2, "For Reporting Quarter", 2, "Two", 2, > "Northern", 100, > + 2, "For Reporting Quarter", 2, "Two", 3, > "Eastern", 106, > + 2, "For Reporting Quarter", 2, "Two", 4, > "Southern", 70, > + 2, "For Reporting Quarter", 3, "Three", 1, > "Western", 119, > + 2, "For Reporting Quarter", 3, "Three", 2, > "Northern", 82, > + 2, "For Reporting Quarter", 3, "Three", 3, > "Eastern", 90, > + 2, "For Reporting Quarter", 3, "Three", 4, > "Southern", 65)) > > testset %>% select(Observation, Label, Value) %>% spread(key=Observation, > value=Value) > # A tibble: 4 x 4 > Label One Three Two > <chr> <dbl> <dbl> <dbl> > 1 Eastern 121 90 106 > 2 Northern 105 82 100 > 3 Southern 74 65 70 > 4 Western 163 119 147 > > > > ## Intended: > > # A tibble: 4 x 4 > For Reporting Quarter One Two Three > <chr> <dbl> <dbl> <dbl> > 1 Western 163 147 119 > 2 Northern 105 100 82 > 3 Eastern 121 106 90 > 4 Southern 74 70 65 > > > > ## > > Unfortunately I don't know how to get there from here. Section, Order and > Seq are there to assist with getting the data to the right output > programmatically, however I don't know how to make use of them. > > Hope this helps. > > -- > Cheers, > Joel Maxuel > > > On Mon, Oct 22, 2018 at 6:18 PM Jeff Newmiller <jdnewmil at dcn.davis.ca.us> > wrote: > > > If you are willing to work in the context of LaTeX output then perhaps > you > > will find the "tables" package useful. However, while you think you have > > communicated clearly enough regarding what you want to accomplish, I do > > not, so either someone else will intuit what you want or you will create > a > > mock-up of what you want your output to look like to remove the > guesswork. > > > > > > [[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. >[[alternative HTML version deleted]]
Joel Maxuel
2018-Oct-23 10:12 UTC
[R] Transformations in Tidyverse (renaming and ordering columns)
I am impressed. Upon playing with factor, I took it a step further to commonize (and this could very well be simpified):> > testset$Observation <- factor(testset$Order, levels=testset$Order,labels=testset$Observation)> testset$Label <- factor(testset$Seq, levels=testset$Seq,labels=testset$Label)> > testset %>% select(Observation, Label, Value) %>%spread(key=Observation, value=Value) # A tibble: 4 x 4 Label One Two Three <fct> <dbl> <dbl> <dbl> 1 Western 163 147 119 2 Northern 105 100 82 3 Eastern 121 106 90 4 Southern 74 70 65>All I see left now is to replace "Label" heading value with the appropriate testset$Section value provided for the situation. In this case... # A tibble: 4 x 4 For Reporting Quarter One Two Three <fct> <dbl> <dbl> <dbl> ... FWIW, before sending this I cleared all formatting and dismissed the formatting toolbar (been doing only the latter before) in the Gmail client, which I presume will finally strip HTML from the message. -- Cheers, Joel Maxuel On Mon, Oct 22, 2018 at 10:48 PM John Laing <john.laing at gmail.com> wrote:> I don't know much about the Tidyverse, but more generally the way to > represent ordered categorical data is with a factor. This seems to work: > > > testset$Observation <- factor(testset$Observation, levels=c("One", > "Two", "Three")) > > testset$Label <- factor(testset$Label, levels=c("Western", "Northern", > "Eastern", "Southern")) > > testset %>% select(Observation, Label, Value) %>% > spread(key=Observation, value=Value) > # A tibble: 4 x 4 > Label One Two Three > <fct> <dbl> <dbl> <dbl> > 1 Western 163 147 119 > 2 Northern 105 100 82 > 3 Eastern 121 106 90 > 4 Southern 74 70 65 > >> >>[[alternative HTML version deleted]]