Skip to content

Instantly share code, notes, and snippets.

@morganmelon
Created September 16, 2016 13:40
Show Gist options
  • Save morganmelon/9de22117ab87f68ccc8f8407341ef0a8 to your computer and use it in GitHub Desktop.
Save morganmelon/9de22117ab87f68ccc8f8407341ef0a8 to your computer and use it in GitHub Desktop.
bookstore lab
#creating columns
title <- c("Morgan Waterman: A Memoir", "Unicorns: The Mystery of the Horn", "Dartmouth Guidebook", "Book Four", "Bakerberry Tales", "Mapmaking", "The Psychoanalysis of Triplets", "Running", "Trump vs. Hillary: The Migration to Canada", "Last But Not Least")
author <- c("Morgan Waterman", "Harry Potter", "Fourth Year Student", "Plane Jane", "Dr. Seuss", "Bartholomeu", "Maria Waterman", "Andie von Eschen", "Stephen Brooks", "Optimist")
ypub <- c(1996, 2020, 2018, 2000, 1964, 1490, 1995, 2002, 2016, 1999)
num <- c(7, 300, 1100, 4, 666, 1, 3, 100, 0, 10)
price <- c(0.01, 10, 99.99, 5, 25, 1000, 17, 15, 24, 1)
#creating data set
bookstore <- data.frame(title, author, ypub, num, price)
#sorting by year published (descending) and author (ascending)
bookstoresorted <- bookstore %>% arrange(-ypub, author)
#discounting for books published in 1996 or earlier or number in stock less than 5
bookstore2 <- bookstoresorted %>% mutate(saleprice = ifelse(ypub<1997, price*0.75, price))
bookstore3 <- bookstore2 %>% mutate(saleprice = ifelse(num<5, price*0.6, saleprice))
bookstore4 <- bookstore3 %>% mutate(saleprice = ifelse(ypub<1997 & num<5, price*0.5, saleprice))
#selecting for discounted books
bookstore5 <- bookstore4 %>% filter(ypub<1997 | num<5)
#displaying only final sale price of discounted books
bookstore6 <- bookstore5 %>% select(-price)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment