I imported a .csv file into a dataset called bestsellers:
bestsellers <- read.csv("AmazonBooks_workingcopy.csv")
Then I created a dataset called YearsPerTitle to discover how many years a book was a bestseller:
YearsPerTitle <- bestsellers %>%
group_by(Name) %>%
summarize(NumberofYears = n())
Then I created a dataset called MultipleYears to return the books that were bestsellers more than once:
MultipleYears <- YearsPerTitle %>%
group_by(Name) %>%
filter(NumberofYears>1)
Now I want to create a dataset called topfifty to return only the top 50 bestsellers in descending order by the NumberofYears they were bestsellers. Here is the code I have:
topfifty <- MultipleYears %>%
filter(NumberofYears>1) %>%
group_by(NumberofYears)
arrange(topfifty, desc(NumberofYears))
head(topfifty,50)
It works until the last line. My tibble says +40 more rows. So it's grabbing only the 50 records I get the error "Error in if ((ni <- n[i]) < 0L) max(d[i] + ni, 0L) else min(ni, d[i]) :
the condition has length > 1"
Also, they are not arranged in descending order by NumberofYears.
I have also tried top_n()
Here are the first 20 rows of my dataset bestsellers:
dput(head(bestsellers, 20))
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")
Here is my dataset YearsPerTitle:
dput(head(YearsPerTitle, 20))
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 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",
"Alexander Hamilton"), NumberofYears = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
Here is MultipleYears:
dput(head(MultipleYears, 20))
structure(list(Name = c("A Man Called Ove: A Novel", "All the Light We Cannot See",
"Becoming", "Between the World and Me", "Brown Bear, Brown Bear, What Do You See?",
"Catching Fire (The Hunger Games)", "Crazy Love: Overwhelmed by a Relentless God",
"Cutting for Stone", "Dear Zoo: A Lift-the-Flap Book", "Diagnostic and Statistical Manual of Mental Disorders, 5th Edition: DSM-5",
"Divergent", "Dog Man: Brawl of the Wild: From the Creator of Captain Underpants (Dog Man #6)",
"Eat to Live: The Amazing Nutrient-Rich Program for Fast and Sustained Weight Loss, Revised Edition",
"Educated: A Memoir", "Fahrenheit 451", "Fifty Shades of Grey: Book One of the Fifty Shades Trilogy (Fifty Shades of Grey Series)",
"First 100 Words", "Game of Thrones Boxed Set: A Game of Thrones/A Clash of Kings/A Storm of Swords/A Feast for Crows",
"Giraffes Can't Dance", "Girl, Wash Your Face: Stop Believing the Lies About Who You Are So You Can Become Who You Were Meant to Be"
), NumberofYears = c(2L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 4L, 2L,
2L, 2L, 2L, 3L, 2L, 2L, 5L, 3L, 6L, 2L)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L), groups = structure(list(
Name = c("A Man Called Ove: A Novel", "All the Light We Cannot See",
"Becoming", "Between the World and Me", "Brown Bear, Brown Bear, What Do You See?",
"Catching Fire (The Hunger Games)", "Crazy Love: Overwhelmed by a Relentless God",
"Cutting for Stone", "Dear Zoo: A Lift-the-Flap Book", "Diagnostic and Statistical Manual of Mental Disorders, 5th Edition: DSM-5",
"Divergent", "Dog Man: Brawl of the Wild: From the Creator of Captain Underpants (Dog Man #6)",
"Eat to Live: The Amazing Nutrient-Rich Program for Fast and Sustained Weight Loss, Revised Edition",
"Educated: A Memoir", "Fahrenheit 451", "Fifty Shades of Grey: Book One of the Fifty Shades Trilogy (Fifty Shades of Grey Series)",
"First 100 Words", "Game of Thrones Boxed Set: A Game of Thrones/A Clash of Kings/A Storm of Swords/A Feast for Crows",
"Giraffes Can't Dance", "Girl, Wash Your Face: Stop Believing the Lies About Who You Are So You Can Become Who You Were Meant to Be"
), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -20L), .drop = TRUE))
Here is topfifty:
dput(head(topfifty, 20))
structure(list(Name = c("A Man Called Ove: A Novel", "All the Light We Cannot See",
"Becoming", "Between the World and Me", "Brown Bear, Brown Bear, What Do You See?",
"Catching Fire (The Hunger Games)", "Crazy Love: Overwhelmed by a Relentless God",
"Cutting for Stone", "Dear Zoo: A Lift-the-Flap Book", "Diagnostic and Statistical Manual of Mental Disorders, 5th Edition: DSM-5",
"Divergent", "Dog Man: Brawl of the Wild: From the Creator of Captain Underpants (Dog Man #6)",
"Eat to Live: The Amazing Nutrient-Rich Program for Fast and Sustained Weight Loss, Revised Edition",
"Educated: A Memoir", "Fahrenheit 451", "Fifty Shades of Grey: Book One of the Fifty Shades Trilogy (Fifty Shades of Grey Series)",
"First 100 Words", "Game of Thrones Boxed Set: A Game of Thrones/A Clash of Kings/A Storm of Swords/A Feast for Crows",
"Giraffes Can't Dance", "Girl, Wash Your Face: Stop Believing the Lies About Who You Are So You Can Become Who You Were Meant to Be"
), NumberofYears = c(2L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 4L, 2L,
2L, 2L, 2L, 3L, 2L, 2L, 5L, 3L, 6L, 2L)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L), groups = structure(list(
NumberofYears = 2:6, .rows = structure(list(c(1L, 2L, 3L,
4L, 8L, 10L, 11L, 12L, 13L, 15L, 16L, 20L), c(5L, 6L, 7L,
14L, 18L), 9L, 17L, 19L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -5L), .drop = TRUE))
I have spent 2 hours on this one line of code, looking at R Help topics galore, and don't know if my logic is wrong or if I just don't know enough about R functions to get it right (I am new to this). Thanks for any help you can give (that works!)