0
votes

I have a large dataset which, simplified, looks something like this:

Year Name January February March April May Street
2000 Bob $100 $197 $124 $100 ABC
2000 Abe $100 $100 $117 $123 $100 ABC
2001 Bob $100 $100 $197 $103 $150 DEF
2001 Abe $140 $100 $127 $526 $123 ABC
2002 Abe $100 $100 $198 $102 $101 DEF
2002 Bob $102 $110 ABC
2003 Carly $100 $100 $197 ABC

I am trying to combine this data so that each person has one line, with the goal of counting and graphing how many months they paid in a row.

I was thinking of trying to recode the data so that each person gets their own row, with a timeline of how much they paid by year and season, with column names like this, but I am having trouble figuring out how to do that.

Name 2000 January 2000 February 2000 March 2000 April 2000 May 2001 January 2001 February 2001 March 2001 April 2001 May 2002 January 2002 February 2002 March 2002 April 2002 May Street

Is there a way to condense variables in this way somehow?

Thank you so much!

1

1 Answers

0
votes

Using pivot_wider from {tidyr} will achieve this. Calling your dataframe yeardata, you can do the following:

selectmonths <- c("January", "February", "March", "April", "May")

result <- yeardata %>%
  pivot_wider(names_from = "Year", values_from = selectmonths)