for
-loop that loops over all numbers between 0 and 10, but only prints numbers below 5.byrow = TRUE
to fill a matrix left-to-right instead of top-to-bottom."Sum > 8"
in the matrix in the cells where that is true.The anscombe
data set is a wonderful data set from 1973 by Francis J. Anscombe aimed to demonstrate that pairs of variables can have the same statistical properties, while having completely differnt graphical representations. We will be using this data set more this week. If you’d like to know more about anscombe
, you can simply call ?anscombe
to enter the help.
You can directly call anscombe
from your console because the datasets
package is a base package in R
. This means that it is always included and loaded when you start an R
instance. In general, when you would like to access functions or data sets from packages that are not automatically loaded, we don’t have to explicitly load the package. We can also call package::thing-we-need
to directly ‘grab’ the thing-we-need
from the package
namespace. For example,
This is especially handy within functions, as we can call package::function-name
to borrow functionality from installed packages, without loading the whole package. Calling only those functions that you need is more memory-efficient than loading it all. More memory efficient means faster computation.
summary
) of each column of the anscombe
dataset from the datasets
packageanscombe
dataset using apply()
.anscombe
dataset using sapply()
. anscombe
dataset using lapply()
. anscombe
. round()
off the means to have a single decimal, and apply
it again to see the results.The mammalsleep
data set from the mice
package shows data collected by Allison and Cicchetti (1976). It holds information for 62 mammal species on the interrelationship between sleep, ecological, and constitutional variables. The dataset contains missing values on five variables, which poses challenges when analyses include these variables.
We will use this datasets also more frequently this week, but we use it only once today. Therefore we could more efficiently call mice::mammalsleep
to obtain only the mammalsleep
data set without loading the whole mice
package.
sd()
) of the vector, if the vector is numeric, or (2) the levels
of the vector, if it is categorical.(a) Apply this function over each column of the mammalsleep
dataset from the mice
package.
(b) Does this function work for all of the columns? If not, fix it.
End of Practical
Allison, T., Cicchetti, D.V. (1976). Sleep in Mammals: Ecological and Constitutional Correlates. Science, 194(4266), 732-734.
Anscombe, Francis J. (1973) Graphs in statistical analysis. American Statistician, 27, 17–21.