0

Hi everyone I am relatively new to R and trying to incorporate some scripts into my work.

I started with a long format table and my goal is to summarize a numeric field (sum) for each state and a corresponding year. Now I have a table with 3 columns: 1) state, 2) year, 3) summarized total with 1,204 rows. This is a tibble table due to my summarize function. For some reason it is also showing my year and totals as double format. Table example

I am now wanting to convert this table to wide format, where each state only has ONE row, with each year having its own column. But I am getting errors when I try to do this and if I convert my tibble back to a data frame, it loses what I've done. I'm so confused! If anyone can help me with this that would be great thank you!

Here is my code:

proj %>% 
  group_by(STATE, YEAR) %>% 
  summarize(total_byState = sum(TOTAL))

proj.wide <- pivot_wider(proj, names_from = YEAR, values_from = TOTAL)

and my error shows as:

Warning message:
Values are not uniquely identified; output will contain list-cols.
* Use `values_fn = list` to suppress this warning.
* Use `values_fn = length` to identify where the duplicates arise
* Use `values_fn = {summary_fun}` to summarise duplicates 

or if I use values_from = total_byState, I get the error:

Error: Can't subset columns that don't exist.
x Column `total_byState` doesn't exist.

0 Answers0