Write an R script that contains statements to address the following numbered ite
ID: 3665824 • Letter: W
Question
Write an R script that contains statements to address the following numbered items.
The function call, rnorm(1), generates a normal random number. Use replicate() to generate 10 normal random numbers and assign them to a vector called nums.
The R statement, data(iris), loads the built-in R data set iris. This dataset has 150 rows and 5 columns. The 5 column names are
"Sepal.Length","Sepal.Width","Petal.Length","Petal.Width" and "Species".
Write R code(s) to return a list of 4 items that correspond to the averages of the first 4 columns in iris. The contents of the list should be as follows:
$Sepal.Length
[1] 5.843333
$Sepal.Width
[1] 3.057333
$Petal.Length
[1] 3.758
$Petal.Width
[1] 1.199333
With the iris dataset still loaded,write R code(s) to return a vector of 4 items that correspond to the averages of the first 4 columns in iris. The contents of the vector should be as follows:
Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
5.843333
3.057333
3.7580000
1.199333
The following R code converts the contents of the first 4 columns in iris to a matrix called data.
data=as.matrix(iris[,1:4])Write R code(s) to use apply() to return a vector that contains the max values of the 4 columns. The contents of the vector should be as follows [Hint, use max()]:
Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
7.9
4.4
6.9
2.5
Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
5.843333
3.057333
3.7580000
1.199333
Explanation / Answer
library(datasets)
data(iris)
iris
?iris
names(iris)
f <- factor(iris$Species)
class(f)
f
#ordered(s)
1. what R code returns a vector of the means of the variables 'Sepal.Length', 'Sepal.Width', 'Petal.Length', and 'Petal.Width'?
colMeans(iris)
apply(iris[, 1:4], 1, mean)
rowMeans(iris[, 1:4])
apply(iris[, 1:4], 2, mean)
#Ans: apply(iris[, 1:4], 2, mean)
#Sepal.Length Sepal.Width Petal.Length Petal.Width
# 5.843333 3.057333 3.758000 1.199333
2.
apply(iris[,1:4],2,max
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.