Skip to content

Instantly share code, notes, and snippets.

@selva86
selva86 / 3_13.R
Created December 25, 2019 01:20
Mini challenge 13
set.seed(100)
clicks_28 <- round(runif (28,3000,4000))
sales_28 <- seq(from=100, to=150, length = 28)
visitors_28 <- runif (28, 1000, 1100)
discount_28 <- rep(c(1,1,1,0.5),7)
mrp <- 5 # maximum retail price
daily_max_revenue <- sales_28 * mrp # daily maximum revenue
daily_actual_revenue <- daily_max_revenue * discount_28 # revenue with discount
total_revenue <- sum(daily_actual_revenue) # total revenue
@selva86
selva86 / 4_5.R
Created January 14, 2020 02:22
Practice exercise for master R course
# 1. Direct assignment
system.time ({
item_id_hypo <- numeric()
for(i in 1:10000000){
item_id_hypo[i] <- i
}
item_id_hypo
})
m1 <- c(7, 4, 4, 14, 8, 14, 8, 1, 4, 1, 13, 5, 12, 13, 11, 5, 15, 1, 7, 4, 8, 4)
m2 <- c(17, 18, 7, 6, 20, 9, 20, 14, 5, 12, 15, 20, 8, 14, 14, 15, 12, 7, 20, 8, 8, 13, 8)
m1
m2
# Find items that are not common between m1 and m2
# 1. From the list m below, get the number of page likes on the 10th day of the month
reviews <- c("spongy burgers", "hot and good","crispier than expected",
"hard to chew", "too large to chew", "takes time", "filling",
"unhealthy but delicious" )
set.seed(100)
pages <- 1:100
page_likes <- round(runif(30,1000,8000),0)
m <- list(reviews,pages,page_likes)
country <- c("France", "Germany", "Greece", "Italy", "Portugal", "Spain") # Countries
gdp_growth <- c(1.3, 0.3, 1.9, 0.3, NA, 2) # GDP growth
mkt_type <- factor(c("High", "High", "Low", "Medium", "Low", "Medium")) # Categories
df <- data.frame(country = country,
gdp_grwth = gdp_growth,
market_typ = mkt_type,
stringsAsFactors = F)
country <- c("France", "Germany", "Greece",
"Italy", "Portugal", "Spain", 'Spain') # Countries
gdp_growth <- c(1.3, 0.3, 1.9, 0.3, NA, 2, 0) # GDP growth
mkt_type <- factor(c("High", "High", "Low", "Medium", "Low", "Medium", 'Low')) # Categories
df <- data.frame(country = country,
gdp_grwth = gdp_growth,
market_typ = mkt_type,
stringsAsFactors = F)
df <- rbind(df, df, df, df)
country <- c("France", "Germany", "Greece", "Italy", "Portugal", "Spain") # Countries
gdp_growth <- c(1.3, 0.3, 1.9, 0.3, NA, 2) # GDP growth
mkt_type <- factor(c("High", "High", "Low", "Medium", "Low", "Medium")) # Categories
df <- data.frame(country = country,
gdp_growth = gdp_growth,
market_typ = mkt_type,
stringsAsFactors = F)
# Add error handling to `largest_hypotenuse()` so it ignores incorrect cases and captures largest
# hypotenuse for eligible cases.
hypotenuse <- function(side1, side2){
side1 <- as.numeric(side1); side2 <- as.numeric(side2)
sqrt(side1^2 + side2^2)
}
# Make a scatterplot with following variables and which looks like this.
# Top half: Points above 150 on Y axis is 'green3' else 'red'.
# Input
set.seed(42)
x <- runif(100, min = 10, max=20)
y <- runif(100, min = 100, max= 200)
# Make a scatterplot with following variables and which looks like this.
# Top half: Points above 150 on Y axis is 'green3' else 'red'.
# Input
set.seed(42)
x <- runif(100, min = 10, max=20)
y <- runif(100, min = 100, max= 200)