pivot_longer not adding data to rows

Note: This is for a class. I'd prefer guidance over an answer if possible.

So I'm new to R. I'm trying to plot the population of Oklahoma from 2010-2019 (and predict 5 years into the future) using a linear regression.

The CSV file that I have has a lot of columns, specifically, the population for each year is in a separate column like

Name, POPULATION2010, POPULATION2011,POPULATION2012, etc.

What I want is for the columns to be:
Name, Year, Population,

I used the following code in hopes of doing that

pops <- read_csv("Desktop/populations/nst-est2019-popchg2010_2019.csv")
OK_pops<- filter(pops, NAME == "Oklahoma")
  pop_OK <- pivot_longer(OK_pops,
  			cols=starts_with("ESTIMATEPOPULATION"),
  			names_to="Year",
  			names_prefix = "yr",
  			values_to = "Population",
  )

What is does is removed all of the POPUATION columns, but it doesn't create the Year and population column that I'm needing

Thank you for you assistance.

Can you please share a small part of the data set in a copy-paste friendly format?

In case you don't know how to do it, there are many options, which include:

  1. If you have stored the data set in some R object, dput function is very handy.

  2. In case the data set is in a spreadsheet, check out the datapasta package. Take a look at this link.

Thanks for the links (especially datapasta)!

I figured it out. It's a rather large files, with many columns. So basically I wasn't scrolling over far enough. Sorry about that. I'm still trying to figure out R Studio!

Thank you again.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.