The apply() function is an alternative to writing loops, via applying a function to columns, rows, or individual values of an array or matrix. The structure of the apply() function is: apply(X, MARGIN, FUN, …) The matrix variable used for the exercises is: dataset1 <- cbind(observationA = 16:8, observationB = c(20:19, 6:12)) Answers to the […]

## Efficient Processing With Apply() Solutions

Below are the solutions to these exercises on Efficient Processing With Apply(). #################### # # # Exercise 1 # # # #################### dataset1 <- cbind(observationA = 16:8, observationB = c(20:19, 6:12)) apply(dataset1, 1, mean) ## [1] 18 17 10 10 10 10 10 10 10 #################### # # # Exercise 2 # # # #################### […]

## Reshape 2 Exercises

The Reshape 2 package is based on differentiating between identification variables, and measurement variables. The functions of the Reshape 2 package then “melt” datasets from wide to long format, and “cast” datasets from long to wide format. Required package: library(reshape2) Answers to the exercises are available here. Exercise 1 Set a variable called “moltenMtcars“, by […]

## Reshape 2 Solutions

Below are the solutions to these exercises on The Reshape 2 Package. #################### # # # Exercise 1 # # # #################### moltenMtcars <- melt(mtcars, id.vars = c("cyl", "gear")) #################### # # # Exercise 2 # # # #################### CarSurvey <- dcast(moltenMtcars, cyl + gear ~ variable, mean) #################### # # # Exercise 3 # […]

## Interactive Subsetting Exercises

The function, “subset()” is intended as a convienent, interactive substitute for subsetting with brackets. subset() extracts subsets of matrices, data frames, or vectors (including lists), according to specified conditions. Answers to the exercises are available here. Exercise 1 Subset the vector, “mtcars[,1]“, for values greater than “15.0“. Exercise 2 Subset the dataframe, “mtcars” for rows […]

## Interactive Subsetting Solutions

Below are the solutions to these exercises on Interactive Subsetting. #################### # # # Exercise 1 # # # #################### subset(mtcars[,1], mtcars[,1] > 15.0) ## [1] 21.0 21.0 22.8 21.4 18.7 18.1 24.4 22.8 19.2 17.8 16.4 17.3 15.2 32.4 ## [15] 30.4 33.9 21.5 15.5 15.2 19.2 27.3 26.0 30.4 15.8 19.7 21.4 #################### […]

## As.Date() Exercises

The as.date() function creates objects of the class “Date“, via input of character representations of dates. Answers to the exercises are available here. Exercise 1 The format of as.Date(x, …) accepts character dates in the format, “YYYY-MM-DD”. For the first exercise, use the c() function, and as.date(), to convert “2010-05-01” and “2004-03-15” to class “date” […]

## As.Date() Solutions

Below are the solutions to these exercises on as.Date Exercise set. #################### # # # Exercise 1 # # # #################### Exer1Dates <- as.Date(c("2010-05-01", "2004-03-15")) #################### # # # Exercise 2 # # # #################### Exer2Date <- as.Date("07/19/98", format = "%m/%d/%y") #################### # # # Exercise 3 # # # #################### as.Date(c(31539, 31540, 31541), origin […]

## Data Shape Transformation With Reshape()

reshape() is an R function that accesses “observations” in grouped dataset columns and “records” in dataset rows, in order to programmatically transform the dataset shape into “long” or “wide” format. Required dataframe: data1 <- data.frame(id=c("ID.1", "ID.2", "ID.3"), sample1=c(5.01, 79.40, 80.37), sample2=c(5.12, 81.42, 83.12), sample3=c(8.62, 81.29, 85.92)) Answers to the exercises are available here. Exercise 1 […]

## Data Shape Transformation With Reshape() Solutions

Below are the solutions to these exercises on Data Shape Transformation With Reshape(). #################### # # # Exercise 1 # # # #################### #Required dataframe: data1 <- data.frame(id=c("ID.1", "ID.2", "ID.3"), sample1=c(5.01, 79.40, 80.37), sample2=c(5.12, 81.42, 83.12), sample3=c(8.62, 81.29, 85.92)) data2 <- reshape(data1, direction="long", varying=2:4, idvar=’id’, timevar="TIME", v.names="Sample") #################### # # # Exercise 2 # # […]