[R] Transformations in Tidyverse (renaming and ordering columns)

John Laing john@|@|ng @end|ng |rom gm@||@com
Tue Oct 23 03:47:50 CEST 2018


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 using 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 using 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 using 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]]




More information about the R-help mailing list