How do I solve the error "can't select within an unnamed vector" when using pivot_longer()?

Hi everyone!

I am trying to run some code in RStudio where I use the pivot_longer() function to create a stacked bar chart from some data, but I keep getting an error message saying "can't select within an unnamed vector."

library(tidyverse)

#>data I am working with
data.frame(
+                             stringsAsFactors = FALSE,
+                                  check.names = FALSE,
+                                `Locale name` = c("Greedgware","Headbon Beach","Nia",
+                                                  "La Droclearlau","Mount Gatier",
+                                                  "Restrol","Port Belltowncalne",
+                                                  "St. Bamprksworth","Ioaks",
+                                                  "Kitchaca"),
+   `% residents who support principal leader` = c(93,48,94,
+                                                  38,91,49,31,6,78,5),
+    `% residents who oppose principal leader` = c(7,52,6,
+                                                  62,9,51,69,94,22,95)
+ )
          Locale name
1          Greedgware
2       Headbon Beach
3                 Nia
4      La Droclearlau
5        Mount Gatier
6             Restrol
7  Port Belltowncalne
8    St. Bamprksworth
9               Ioaks
10           Kitchaca
   % residents who support principal leader
1                                        93
2                                        48
3                                        94
4                                        38
5                                        91
6                                        49
7                                        31
8                                         6
9                                        78
10                                        5
   % residents who oppose principal leader
1                                        7
2                                       52
3                                        6
4                                       62
5                                        9
6                                       51
7                                       69
8                                       94
9                                       22
10                                      95

#>my code
locale_name <- dataset[["Locale name"]]
pSupport <- dataset[["% residents who support principal leader"]]
pOppose <- dataset[["% residents who oppose principal leader"]]

df <- structure(list(locale_name, pSupport, pOppose),  row.names = c(NA, 10),  class = c("tbl_df", "tbl", "data.frame"))

pivot_longer(df, c("pSupport", "pOppose"), names_to = "type")
#>Error: Can't select within an unnamed vector

Does anyone know what I am doing wrong?

pivot_longer(dataset, c("% residents who support principal leader",
                        "% residents who oppose principal leader"), 
names_to = "type")

Did you have some specific intent with

locale_name <- dataset[["Locale name"]]
pSupport <- dataset[["% residents who support principal leader"]]
pOppose <- dataset[["% residents who oppose principal leader"]]

df <- structure(list(locale_name, pSupport, pOppose),  row.names = c(NA, 10),  class = c("tbl_df", "tbl", "data.frame"))

because this does seem like an odd way to deconstruct and then reconstruct the original information, but with a consequence of losing the column names along the way.
maybe dplyr::rename (or even select()) would have suited if you no longer wanted the long column names.

I am trying to recreate the code written by andresrcs here, but with my own data.

Am I correct in thinking that the reason why my code failed is that I needed to use the column names as formals, but the pSupport and pOppose variables are just containers for the column data, not the names of the columns?

Is this what you are trying to do?

library(tidyverse)

sample_df <- data.frame(stringsAsFactors = FALSE,
                        check.names = FALSE,
                        `Locale name` = c("Greedgware","Headbon Beach","Nia",
                                          "La Droclearlau","Mount Gatier",
                                          "Restrol","Port Belltowncalne",
                                          "St. Bamprksworth","Ioaks",
                                          "Kitchaca"),
                        `% residents who support principal leader` = c(93,48,94,
                                                                       38,91,49,31,6,78,5),
                        `% residents who oppose principal leader` = c(7,52,6,
                                                                      62,9,51,69,94,22,95)
)

sample_df %>%
    rename(pSupport = `% residents who support principal leader`,
           pOppose = `% residents who oppose principal leader`) %>% 
    pivot_longer( -`Locale name`, names_to = "position", values_to = "percentage") %>% 
    ggplot(aes(x = percentage, y = `Locale name`, fill = position)) +
    geom_col() +
    scale_x_continuous(labels = scales::percent_format(scale = 1))

Created on 2021-12-15 by the reprex package (v2.0.1)

I'm sorry but this phrase is not clear to me, I don't understand what you are referring to, I think you are misunderstanding what the arguments for the pivot_longer() function should be, maybe this article would clarify things for you

Also, please test your reprex before posting, the one you have posted here had syntax errors that made it a little harder to take a look into your issue.

1 Like

This topic was automatically closed 7 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.