Can't subset columns that don't exist even if they do! Testing on windows

library(magrittr)
dataValues <- data.frame(dataElement = rnorm(10),
                         value = 1:10)

dataValues %>% 
  tidyr::pivot_wider(., id_cols = dataElement, names_from = dataElement, values_from = value)
#> Error in `stop_subscript()`:
#> ! Can't subset columns that don't exist.
#> x Column `dataElement` doesn't exist.
reprex::reprex()
#> i Non-interactive session, setting `html_preview = FALSE`.
#> CLIPR_ALLOW has not been set, so clipr will not run interactively
#> Error in switch(where, expr = stringify_expression(x_expr), clipboard = ingest_clipboard(), : EXPR must be a length 1 vector

Created on 2022-02-11 by the reprex package (v2.0.1)

You have to quote the variable names

dataValues %>% 
  tidyr::pivot_wider(., id_cols = "dataElement" , names_from = "dataElement", values_from = "value")

quoting the variables doesn't work:

library(magrittr)
dataValues <- data.frame(dataElement = rnorm(10),
                         value = 1:10)

dataValues %>% 
  tidyr::pivot_wider(., id_cols = "dataElement", names_from = "dataElement", values_from = "value")
#> Error in `stop_subscript()`:
#> ! Can't subset columns that don't exist.
#> x Column `dataElement` doesn't exist.


reprex::reprex()
#> i Non-interactive session, setting `html_preview = FALSE`.
#> CLIPR_ALLOW has not been set, so clipr will not run interactively
#> Error in switch(where, expr = stringify_expression(x_expr), clipboard = ingest_clipboard(), : EXPR must be a length 1 vector

Created on 2022-02-11 by the reprex package (v2.0.1)

But without specifying the id_cols it works!

library(magrittr)
dataValues <- data.frame(dataElement = rnorm(10),
                         value = 1:10)

dataValues %>% 
  tidyr::pivot_wider(., names_from = dataElement, values_from = value)
#> # A tibble: 1 x 10
#>   `0.826986943418365` `-0.5214790113857` `-0.0650241880911871` `-0.84807319528~`
#>                 <int>              <int>                 <int>             <int>
#> 1                   1                  2                     3                 4
#> # ... with 6 more variables: `-0.224404300418023` <int>,
#> #   `-1.31788368455247` <int>, `0.0915744709159496` <int>,
#> #   `-1.33344284491811` <int>, `0.0647617762545832` <int>,
#> #   `-0.22658374043902` <int>


reprex::reprex()
#> i Non-interactive session, setting `html_preview = FALSE`.
#> CLIPR_ALLOW has not been set, so clipr will not run interactively
#> Error in switch(where, expr = stringify_expression(x_expr), clipboard = ingest_clipboard(), : EXPR must be a length 1 vector

Created on 2022-02-11 by the reprex package (v2.0.1)

Check out the documentation for the id_cols argument in pivot_wider(), in particular its function and the default values.

A small thing, but the magrittr pipe assigns the LHS to the first argument of the RHS function or where the . placeholder appears. For pivot_wider() the first argument is data, so there is no need to include ., in the first position.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.