1/22
Looks like no tags are added yet.
Calculate the product of x and y
x * y
Store the result in a new object called z
z ← x * y
Find the minimum, maximum, length, and variance of myvec.
min(myvec), max(myvec), length(myvec), var(myvec)
The numbers below are the first ten days of rainfall amounts in 1996. Read them into a vector using the c() function.
rainfall ← c(put the first number here, put the second number here, ...)
Type the code for the mean rainfall, how about the standard deviation?
mean(rainfall)
sd(rainfall)
Which day saw the highest rainfall (write code)?
which.max(rainfall)
How do we write comments in an R script?
# This is a comment
You have measured five cylinders, their lengths are: 2.1, 3.4, 2.5, 2.7, 2.9 and the diameters are : 0.3, 0.5, 0.6, 0.9, 1.1
Write the code for the correlation between lengths and diameters (use the cor function)
lengths ← c(2.1, 3.4, 2.5, 2.7, 2.9) diameters <- c(0.3, 0.5, 0.6, 0.9, 1.1) cor(lengths, diameters)
You have measured five cylinders, their lengths are: 2.1, 3.4, 2.5, 2.7, 2.9 and the diameters are : 0.3, 0.5, 0.6, 0.9, 1.1.
Write the code for the volume of each cylinder (V = length * pi * (diameter / 2) 2 ).
volume ← lengths * pi * (diameters / 2)^2
Draw 10 random letters from the lowercase alphabet, and sort them alphabetically (Hint: use sample and sort). The solution can be one line of code.
sort(sample(letters, 10))
install.packages("car")
install.packages("car")
Write the code to read in the dataset "Cereals.csv", look at the first few rows with head and inspect the data types of the variables in the dataframe with str.
Cereals ← read.csv("Cereals.csv") head(Cereals)\str(Cereals)
Given the dataset : titanic ← read.table("titanic.txt", header=TRUE)
Write the code to convert the 'Name' (passenger name) variable to a 'character' variable, and store it in the dataframe.
titanicName←as.character(titanicName)
Make a new variable called 'Status', based on the 'Survived' variable already in the dataset. For passengers that did not survive, Status should be 'dead', for those who did, Status should be 'alive'. Make sure this new variable is a factor. Use "ifelse".
titanicStatus←factor(ifelse(titanicSurvived == 0, "dead", "alive"))
Create a scatter plot showing the relationship between the variables mpg (miles per gallon) and hp (horsepower). Add a title, axis labels, and use a color different from the default.
plot(mtcarshp,mtcarsmpg,
main = "Miles per Gallon vs. Horsepower",
xlab = "Horsepower",
ylab = "Miles per Gallon (MPG)",
col = "blue",
pch = 19)
Create a line plot showing the relationship between wt (weight) and mpg (miles per gallon). Make sure to customize the plot with a title, axis labels, and a line color of your choice.
plot(mtcarswt,mtcarsmpg,
type = "l",
main = "Miles per Gallon vs. Weight",
xlab = "Weight (1000 lbs)",
ylab = "Miles per Gallon (MPG)",
col = "red")
Create a histogram of the mpg variable (miles per gallon). Use the viridis color palette for the fill color. Customize the bin width and add appropriate labels.
ggplot(mtcars, aes(x = mpg)) + geom_histogram(binwidth = 3, fill = viridis(1),
color = "black") +
labs(
title = "Distribution of Miles per Gallon",
x = "Miles per Gallon (MPG)",
y = "Frequency" ) +
theme_minimal()
Create a box plot comparing mpg across the number of cylinders (cyl). Use viridis to color the plot by the number of cylinders. Customize the plot with a title, axis labels, and theme.
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(cyl))) +
geom_boxplot() +
scale_fill_viridis_d() +
labs(
title = "Miles per Gallon by Number of Cylinders",
x = "Number of Cylinders",
y = "Miles per Gallon (MPG)",
fill = "Cylinders" ) +
theme_minimal()
Create a bar chart that shows the count of cars with different numbers of cylinders (cyl). Customize the plot with a title, labels, and choose a color palette of your preference (e.g., viridis, brewer, etc.).
ggplot(mtcars, aes(x = factor(cyl), fill = factor(cyl))) + geom_bar() +
scale_fill_viridis_d() +
labs(
title = "Count of Cars by Number of Cylinders",
x = "Number of Cylinders",
y = "Number of Cars",
fill = "Cylinders" ) +
theme_minimal()
For this question, use the pupae data. Write the code that perform a simple linear regression of Frass on PupalWeight
model ← lm(Frass ~ PupalWeight, data = pupae)
What is the code for the summary of the model?
summary(model)
Using the Pulse data, write the code to fit a multiple linear regression of Pulse1 against Age, Weight and Height.
model ← lm(Pulse1 ~ Age + Weight + Height, data = Pulse)
Using the Pulse data once more, write the code that builds a model to see if Pulse2 can predict whether people were in the Ran group. Make sure that Ran is coded as a factor.
PulseRan←as.factor(PulseRan) model <- glm(Ran ~ Pulse2, data = Pulse, family = binomial) summary(model)