Home > Mobile >  Error in `vec_as_location()`: using pivot_wider
Error in `vec_as_location()`: using pivot_wider

Time:11-29

I have data that looks like this;

v<-as.numeric(c(rep(c(1,2,3), 3)))
n<-as.factor(c(rep("a", 3), rep("b", 3), rep("c", 3)))
t<-data.frame(v, n)
str(t)

I am using the tidyr version 1.1.4. inside RStudio version 2021.9.0.351. I am using pivot_wider() to go from long to wide format like this;

t.wide<-t%>%pivot_wider(names_from=n, values_from=v)

But I get this error code;

Error in `vec_as_location()`:
! `...` must be empty.
✖ Problematic argument:
• call = call

Can anyone please help me?

CodePudding user response:

The error is likely related to a problem with the vctrs package; updating the package with install.packages("vctrs") may fix the issue. For the pivot, you can use:

library(tidyr)

v<-as.numeric(c(rep(c(1,2,3), 3)))
n<-as.factor(c(rep("a", 3), rep("b", 3), rep("c", 3)))
t<-data.frame(v, n)
str(t)
#> 'data.frame':    9 obs. of  2 variables:
#>  $ v: num  1 2 3 1 2 3 1 2 3
#>  $ n: Factor w/ 3 levels "a","b","c": 1 1 1 2 2 2 3 3 3

t %>%
  pivot_wider(names_from = n,
              values_from = v,
              values_fn = list) %>%
  unnest(cols = c(a, b, c))
#> # A tibble: 3 × 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     1     1     1
#> 2     2     2     2
#> 3     3     3     3

Created on 2022-11-29 with reprex v2.0.2

  • Related