Error in `pivot_longer()`: ! Can't combine `Stock1_name` <character> and `Stock1_dollars` <integer>

24 views Asked by At

I currently have wide data that I'm trying to convert to long data. In wide, it's organized in these columns: ID | PlanName | Year | State | Stock1_name | Stock1_dollars | Stock2_name | Stock2_dollars

This continues all the way to Stock10_dollars.

I'd like to tidy this so it's organized like this: ID, PlanName, Year, State, Stock, Dollars

I run the code below and get the error:

Error in pivot_longer(): ! Can't combine Stock1_name and Stock1_dollars .

Any help would be greatly appreciated. I'm new to R and just running into a wall trying to figure this out.

after_data <- before_data %>%
  pivot_longer(
    cols = starts_with("Stock"),
    names_to = c(".value", "name"),
    names_sep = "_"
  ) %>%
  select(-name)
0

There are 0 answers