The aggregate() function subsets dataframes, and time series data, then computes summary statistics. The structure of the aggregate() function is aggregate(x, by, FUN). Answers to the exercises are available here. Exercise 1 Aggregate the “airquality” data by “airquality$Month“, returning means on each of the numeric variables. Also, remove “NA” values. Exercise 2 Aggregate the “airquality” […]

## Summary Statistics With Aggregate() Solutions

Below are the solutions to these exercises on Summary Statistics with Aggregate(). #################### # # # Exercise 1 # # # #################### aggregate(airquality, list(airquality$Month), mean, na.rm=T) ## Group.1 Ozone Solar.R Wind Temp Month Day ## 1 5 23.61538 181.2963 11.622581 65.54839 5 16.0 ## 2 6 29.44444 190.1667 10.266667 79.10000 6 15.5 ## 3 7 […]

## Scripting Loops In R

An R programmer can determine the order of processing of commands, via use of the control statements; repeat{}, while(), for(), break, and next Answers to the exercises are available here. Exercise 1 The repeat{} loop processes a block of code until the condition specified by the break statement, (that is mandatory within the repeat{} loop), […]

## Scripting Loops in R Solutions

[emaillocker]Below are the solutions to these exercises on Scripting Loops in R. #################### # # # Exercise 1 # # # #################### i <- 0 repeat{ i <- i + 2 print(i) if(i == 10) { break } } ## [1] 2 ## [1] 4 ## [1] 6 ## [1] 8 ## [1] 10 #################### […]

## Accessing Dataframe Objects Exercises

The attach() function alters the R environment search path by making dataframe variables into global variables. If incorrectly scripted, the attach() function might create symantic errors. To prevent this possibility, detach() is needed to reset the dataframe objects in the search path. The transform() function allows for transformation of dataframe objects. The within() function creates […]

## Accessing Dataframe Objects Solutions

Below are the solutions to these exercises on Accessing Dataframe Objects. #################### # # # Exercise 1 # # # #################### attach(buildingSurvey) summary(floors) ## Min. 1st Qu. Median Mean 3rd Qu. Max. ## 5.000 8.500 10.000 9.333 10.750 12.000 #################### # # # Exercise 2 # # # #################### summary(efficiency) ## Min. 1st Qu. Median […]

## Cross Tabulation with Xtabs exercises

The xtabs() function creates contingency tables in frequency-weighted format. Use xtabs() when you want to numerically study the distribution of one categorical variable, or the relationship between two categorical variables. Categorical variables are also called “factor” variables in R. Using a formula interface, xtabs() can create a contingency table, (also a “sparse matrix”), from cross-classifying […]

## Cross Tabulation with Xtabs Solutions

Below are the solutions to these exercises on xtabs(). #################### # # # Exercise 1 # # # #################### Data1 <- data.frame(Reference = c(“KRXH”, “KRPT”, “FHRA”, “CZKK”, “CQTN”, “PZXW”, “SZRZ”, “RMZE”, “STNX”, “TMDW”), Status = c(“Accepted”, “Accepted”, “Rejected”, “Accepted”, “Rejected”, “Accepted”, “Rejected”, “Rejected”, “Accepted”, “Accepted”), Gender = c(“Female”, “Male”, “Male”, “Female”, “Female”, “Female”, “Male”, “Female”, […]

## Complex Tables – Exercises

The ftable() function combines Cross-Tabulation with the ability to format , or “flatten”, contingency tables of 3 or more dimensions. The resulting tables contain the combined counts of the categorical variables, (also factor variables in R), that are then arranged as a matrix, whose rows and columns correspond to the original data’s rows and columns. […]

## Complex tables: solutions

Below are the solutions to these exercises on complex tables. #################### # # # Exercise 1 # # # #################### ftable(Titanic) ## Survived No Yes ## Class Sex Age ## 1st Male Child 0 5 ## Adult 118 57 ## Female Child 0 1 ## Adult 4 140 ## 2nd Male Child 0 11 ## […]