In this R markdown document, you should answer each question and click the knit button to generate a pdf or HTML or word document, which should be submitted into D2L.
Type answers after the -> sign.
The ChickWeight data frame has 578 rows and 4 columns
from an experiment on the effect of diet on early growth of chicks. The
body weights of the chicks were measured at birth and every second day
thereafter until day 20. They were also measured on day 21. There were
four groups on chicks on different protein diets.
library(datasets)
data("ChickWeight")
str(ChickWeight)
## Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame': 578 obs. of 4 variables:
## $ weight: num 42 51 59 64 76 93 106 125 149 171 ...
## $ Time : num 0 2 4 6 8 10 12 14 16 18 ...
## $ Chick : Ord.factor w/ 50 levels "18"<"16"<"15"<..: 15 15 15 15 15 15 15 15 15 15 ...
## $ Diet : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
## - attr(*, "formula")=Class 'formula' language weight ~ Time | Chick
## .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
## - attr(*, "outer")=Class 'formula' language ~Diet
## .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
## - attr(*, "labels")=List of 2
## ..$ x: chr "Time"
## ..$ y: chr "Body weight"
## - attr(*, "units")=List of 2
## ..$ x: chr "(days)"
## ..$ y: chr "(gm)"
-> Factor (nominal)
-> Factor (continuous)
-> ord. factor (ordinal)
-> Num (continuous)
#Get help from R and find information about Data
?ChickWeight
-> The ChickWeight data frame has 578 rows and 4 columns from an experiment on the effect of diet on early growth of chicks
names(ChickWeight)
## [1] "weight" "Time" "Chick" "Diet"
-> names of variables
dim(ChickWeight) # dimension of dataset
## [1] 578 4
nrow(ChickWeight) # number of rows
## [1] 578
ncol(ChickWeight) # number of columns
## [1] 4
-> 578
-> 4
subchick <- ChickWeight[, c("weight", "Chick")] # subsetting the original dataset
-> subchick
-> 2 variable: “weight” and “chick”
Now, we practice how to import data from various file types in into
R.
First, write the dataset to your local computer hard disk by update
the directory path: /ChickWeight.csv to your own computer
path. Here is the function to get the working directory if you want to
use your currect working directory:
getwd()
## [1] "/Users/caliplayer/Downloads"
#COPY THE OUTPUT FROM THE CHUNK WITH getwd() AND PASTE IT BETWEEN "/ IN THE CODE BELOW
write.csv(ChickWeight, "/Users/caliplayer/Downloads/ChickWeight.csv", row.names = FALSE)
To read this data into R, we would use the
read.csv() function. Please update the directory path:
“/ChickWeight.csv” to your own computer path.
#COPY THE OUTPUT FROM THE CHUNK WITH getwd() AND PASTE IT BETWEEN "/ IN THE CODE BELOW
ChickWeight_data_from_csv = read.csv("/Users/caliplayer/Downloads/ChickWeight.csv")
Now you can use the function head() and other functions
we introduced as above to explore the basic features of the imported
dataset: ChickWeight_data_from_csv.
head(ChickWeight_data_from_csv, n = 10)
->weight