Counting Number of Years a Name Appears in a dataset

bestsellers %>%
group_by(Name) %>%
summarize(NumberofYears = count(Year))

My dataset is called bestsellers. Both Name and Year are columns in my dataset. This is my error message: Error in summarize():
:information_source: In argument: NumberofYears = count(Year).
:information_source: In group 1: Name = "10-Day Green Smoothie Cleanse".
Caused by error in UseMethod():
! no applicable method for 'count' applied to an object of class "c('integer', 'numeric')"
Run rlang::last_error() to see where the error occurred.

Please help. Thanks!

Could you upload a small sample of your data? You can provide the first 20 rows of your data by posting the output of

dput(head(bestsellers,20))

If there are many columns in the data set, it would be enough to post only the Name and Year columns.

dput(bestsellers[1:20, c("Name", "Year"])

Put lines with three back ticks just before and after the dput() output, like this
```
dput() output goes here
```

structure(list(Name = c("10-Day Green Smoothie Cleanse", "11/22/63: A Novel",
"12 Rules for Life: An Antidote to Chaos", "1984 (Signet Classics)",
"5,000 Awesome Facts (About Everything!) (National Geographic Kids)",
"A Dance with Dragons (A Song of Ice and Fire)", "A Game of Thrones / A Clash of Kings / A Storm of Swords / A Feast of Crows / A Dance with Dragons",
"A Gentleman in Moscow: A Novel", "A Higher Loyalty: Truth, Lies, and Leadership",
"A Man Called Ove: A Novel", "A Man Called Ove: A Novel", "A Patriot's History of the United States: From Columbus's Great Discovery to the War on Terror",
"A Promised Land", "A Stolen Life: A Memoir", "A Wrinkle in Time (Time Quintet)",
"Act Like a Lady, Think Like a Man: What Men Really Think About Love, Relationships, Intimacy, and Commitment",
"Adult Coloring Book Designs: Stress Relief Coloring Book: Garden Designs, Mandalas, Animals, and Paisley Patterns",
"Adult Coloring Book: Stress Relieving Animal Designs", "Adult Coloring Book: Stress Relieving Patterns",
"Adult Coloring Books: A Coloring Book for Adults Featuring Mandalas and Henna Inspired Flowers, Animals, and Paisley"
), Author = c("JJ Smith", "Stephen King", "Jordan B. Peterson",
"George Orwell", "National Geographic Kids", "George R. R. Martin",
"George R. R. Martin", "Amor Towles", "James Comey", "Fredrik Backman",
"Fredrik Backman", "Larry Schweikart", "Barack Obama", "Jaycee Dugard",
"Madeleine L'Engle", "Steve Harvey", "Adult Coloring Book Designs",
"Blue Star Coloring", "Blue Star Coloring", "Coloring Books for Adults"
), Price = c("$8.00 ", "$22.00 ", "$15.00 ", "$6.00 ", "$12.00 ",
"$11.00 ", "$30.00 ", "$15.00 ", "$3.00 ", "$8.00 ", "$8.00 ",
"$2.00 ", "$23.00 ", "$32.00 ", "$5.00 ", "$17.00 ", "$4.00 ",
"$6.00 ", "$6.00 ", "$8.00 "), Year = c(2016L, 2011L, 2018L,
2017L, 2019L, 2011L, 2014L, 2017L, 2018L, 2016L, 2017L, 2010L,
2020L, 2011L, 2018L, 2009L, 2016L, 2015L, 2015L, 2015L), Genre = c("Non-fiction",
"Fiction", "Non-fiction", "Fiction", "Non-fiction", "Fiction",
"Fiction", "Fiction", "Non-fiction", "Fiction", "Fiction", "Non-fiction",
"Non-fiction", "Non-fiction", "Fiction", "Non-fiction", "Non-fiction",
"Non-fiction", "Non-fiction", "Non-fiction")), row.names = c(NA,
20L), class = "data.frame")

I think you want n() rather than count() in summarize().

When I use n() instead of count() I get the following error:

Error in summarize():
:information_source: In argument: NumberofYears = n(Year).
:information_source: In group 1: Name = "10-Day Green Smoothie Cleanse".
Caused by error in n():
! unused argument (Year)

Try

bestsellers %>%
group_by(Name) %>%
summarize(NumberofYears = n())

Unless you have some guarantees about how Years are structured into the data, the most robust approach is probably

bestsellers %>%
  group_by(Name) |> 
  summarise(NumberofYears=length(unique(Year)))

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.