Home > Back-end >  Pivot longer is showing transformed dataframe
Pivot longer is showing transformed dataframe

Time:10-11

I tried to create longer format of a dataset and I am getting a transformed dataframe. I have seen that the column names are in reverse format. But don't know how to fix this. I want x, y are column names. any help?

library(pacman)
p_load(tidyverse, purrr, datasauRus)
datasaurus_dozen_wide %>% 
  pivot_longer(everything(),
               names_to = c(".value", "set"),
               names_pattern = "(.*)_(.)")
#> # A tibble: 284 × 14
#>    set    away bullseye circle  dino  dots h_lines high_…¹ slant…² slant…³  star
#>    <chr> <dbl>    <dbl>  <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <dbl>
#>  1 x      32.3     51.2   56.0  55.4  51.1    53.4    57.6    52.9    47.7  58.2
#>  2 y      61.4     83.3   79.3  97.2  90.9    90.2    83.9    97.3    95.2  91.9
#>  3 x      53.4     59.0   50.0  51.5  50.5    52.8    51.3    59.0    44.6  58.2
#>  4 y      26.2     85.5   79.0  96.0  89.1    90.1    82.8    93.6    93.1  92.2
#>  5 x      63.9     51.9   51.3  46.2  50.2    47.1    50.8    56.4    43.9  58.7
#>  6 y      30.8     85.8   82.4  94.5  85.5    90.5    76.8    96.3    94.1  90.3
#>  7 x      70.3     48.2   51.2  42.8  50.1    42.4    37.0    37.8    41.6  57.3
#>  8 y      82.5     85.0   79.2  91.4  83.1    89.5    82.0    94.4    90.3  89.9
#>  9 x      34.1     41.7   44.4  40.8  50.6    42.7    42.9    39.9    49.2  58.1
#> 10 y      45.7     84.0   78.2  88.3  82.9    90.4    80.2    90.6    96.6  92.0
#> # … with 274 more rows, 3 more variables: v_lines <dbl>, wide_lines <dbl>,
#> #   x_shape <dbl>, and abbreviated variable names ¹​high_lines, ²​slant_down,
#> #   ³​slant_up

Created on 2022-10-10 with reprex v2.0.2

CodePudding user response:

You could achieve your desired result by simply switching ".value" and "set" in the names_to argument:

library(tidyr)
library(datasauRus)

datasaurus_dozen_long <- datasaurus_dozen_wide %>% 
  pivot_longer(everything(),
               names_to = c("set", ".value"),
               names_pattern = "(.*)_(.)")

head(datasaurus_dozen_long)
#> # A tibble: 6 × 3
#>   set          x     y
#>   <chr>    <dbl> <dbl>
#> 1 away      32.3  61.4
#> 2 bullseye  51.2  83.3
#> 3 circle    56.0  79.3
#> 4 dino      55.4  97.2
#> 5 dots      51.1  90.9
#> 6 h_lines   53.4  90.2

library(ggplot2)

ggplot(datasaurus_dozen_long, aes(x, y))  
  geom_point()  
  facet_wrap(~set)

  • Related