Skip to content

Instantly share code, notes, and snippets.

options(scipen=999)
set.seed(100)
year <- 2001:2050 # X Axis
population <- 7000 + (1:50 * runif(50, .5, .55) * 100) # Y Axis 1
econ <- sample(40:70, 50, replace = T) # Y Axis 2
# 1. You have a vector which contains four coupons codes for Asian flights.
# Replace all 'asia' with 'europe'
flight_coupons <- c("asiafly", "asiaflynow", "asiaflymiles", "asiamarch")
# 2. Extract the first names from the emails.
emails <- c("vito.corleone@apple.com", "michael.corleone@reddit.com")
# Input
set.seed(100)
first <- sample(10:20, 15, replace = T) # first sides of RHT
second <- sample(10:20, 15, replace = T) # second sides of RHT
# Final Result updates here
largest_val <- 0
# Calc Hypotenuse
hypotenuse <- function(side1, side2){
print_1_to_10 <- function(){
for(i in 1:10){
print(i)
}
}
# Add error handling to `largest_hypotenuse()` so it ignores incorrect cases and captures largest
# hypotenuse for eligible cases.
source('https://bit.ly/2w2WA9k')
# OR
hypotenuse <- function(side1, side2){
side1 <- as.numeric(side1); side2 <- as.numeric(side2)
sqrt(side1^2 + side2^2)
set.seed(100)
df1 <- data.frame(a=rep(1:3, each=4), b=round(runif(12, 10, 20)))
df2 <- data.frame(a=0:1, c=10:11)
# You have two dataframe df1 and df2 from the code below. df1 and df2 have column a in common.
# The challenge for this video is as follows:
# 1. From df1, remove the rows that are present in df2, based on the common column a.
# 2. Create another dataframe df3 that contains all the rows and columns from both datasets.
set.seed(100)
M <- matrix(round(runif(100*100), 2), nrow=100, ncol=100)
df <- as.data.frame(M)
df[1:5, 1:5]
# 1. Replace the diagonal of dataframe `df` to the respective row number
# using the set function.
# 2. Set the values of column V1 to values of column V2, for all rows where V2 is
# Run the below code to get the graph. The graph represents speed of car
# vs distance it travels before stopping (when brakes are applied)
# In this graph, write an insight stating that "The faster the car,
# the longer it takes to stop"
gg <- ggplot(data=cars, aes(x=speed, y=dist, size=dist)) +
geom_point() +
geom_smooth() +
labs(title="Cars", x="Speed", y="Dist")
print(gg)
# Modify the following code to remove the legend title. Then, place the legend in top left position
gg <- ggplot(midwest, aes(x=area, y=poptotal)) + # Define Data
geom_point(aes(col=state, size=popdensity)) + # Add scatterplot
geom_smooth(method="lm", col="firebrick", se=F) + # Add best fit line
coord_cartesian(xlim=c(0, 0.1), ylim=c(0, 250000)) + # Limit X and Y axis
labs(title="Area Vs Population", y="Population", x="Area") # Labels
plot(gg)
# Input
library(ggplot2)
data(mpg, package="ggplot2")
mpg <- read.csv("http://goo.gl/uEeRGu")
g <- ggplot(mpg, aes(x=displ, y=hwy)) +
geom_point() +
geom_smooth(method="lm") +
theme_bw()
plot(g)