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?