[In the exercises below we cover the basics of data frames.] Answers to the exercises are available here. Exercise 1 Consider two vectors: x=seq(1,43,along.with=Id) y=seq(-20,0,along.with=Id) Create a data.frame df: >df Id Letter x y 1 1 a 1.000000 -20.000000 2 1 b 4.818182 -18.181818 3 1 c 8.636364 -16.363636 4 2 a 12.454545 -14.545455 5 […]

# lists and dataframes

## Data Frame exercises Vol. 2 : solutions

Below are the solutions to these exercises on regular sequences. #################### # # # Exercise 1 # # # #################### # Consider two vectors: # x=seq(1,43,along.with=Id) # y=seq(-20,0,along.with=Id) # Create a data.frame df # # >df # Id Letter x y # 1 1 a 1.000000 -20.000000 # 2 1 b 4.818182 -18.181818 # 3 […]

## Applying Functions To Lists Exercises

The lapply() function applies a function to individual values of a list, and is a faster alternative to writing loops. Structure of the lapply() function: lapply(LIST, FUNCTION, …) The list variable used for these exercises: list1 <- list(observationA = c(1:5, 7:3), observationB=matrix(1:6, nrow=2)) Answers to the exercises are available here. Exercise 1 Using lapply(), find […]

## 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 […]

## Merging Dataframes Exercises

When combining separate dataframes, (in the R programming language), into a single dataframe, using the cbind() function usually requires use of the “Match()” function. To simulate the database joining functionality in SQL, the “Merge()” function in R accomplishes dataframe merging with the following protocols; “Inner Join” where the left table has matching rows from one, […]

## Data frame exercises

In the exercises below we cover the basics of data frames. Before proceeding, first read section 6.3.1 of An Introduction to R, and the help pages for the cbind, dim, str, order and cut functions. Answers to the exercises are available here. Exercise 1 Create the following data frame, afterwards invert Sex for all individuals. […]

## List exercises

In the exercises below we cover the basics of lists. Before proceeding, first read section 6.1-6.2 of An Introduction to R, and the help pages for the sum, length, strsplit, and setdiff functions. Answers to the exercises are available here. Exercise 1 If: p Are you a beginner (1 star), intermediate (2 stars) or advanced […]