Q1:
Take a look at the 'iris' dataset that comes with R. The data can be loaded with the code:
> library(datasets)
> data(iris)
A description of the dataset can be found by running
>?iris
There will be an object called 'iris' in your workspace. In this dataset, what is the mean of 'Sepal.Length' for the species virginica? Please round your answer to the nearest whole number.(取整数)
(Only enter the numeric result and nothing else.)
ANSWER:
#直接使用mean函数,mean(x),用[]来取特定值
> mean(iris[iris$Species == "virginica",]$Sepal.Length)
[1] 6.588
or:
#或者使用tapply函数,tapply(vector, index, function):
> tapply(iris$Sepal.Length,iris$Species=="virginica",mean)
FALSE TRUE