[For this exercise, first write down your answer, without using R. Then, check your answer using R.]

Answers to the exercises are available here.

**Exercise 1**

Create a function that given a data frame and a vector, will add a the vector (if the vector length match with the rows number of the data frame)

as a new variable to the data frame.

**Exercise 2**

Consider a data frame df:

Id=c(1:10)

Age=c(14,12,15,10,23,21,41,56,78,12)

Sex=c('F','M','M','F','M','F','M','M','F','M')

Code=letters[1:10]

df=data.frame(Id,Age,Sex,Code)

Create a function that, given a data frame and two indexes, exchanges two values โโof the Code variable with each other.

For example, if the index is 1 and 3, you assign:

df[1,'Code']=df[3,'Code']

df[3,'Code']=df[1,'Code']

**Exercise 3**

Consider two variables x,y and a data frame df:

x,y integer

`A=c(1:10)`

B=seq(100,10,-10)

H=seq(-200,-50,along.with=B)

df=data.frame(A,B,H)

Create a function that given a data frame df calculate a new variable ‘SUM_x_y'(If x=2 and y=3, then the new variable will be ‘SUM_2_3’,

if x=4 and y=10, then the new variable will be ‘SUM_4_10’),such that for each row ‘i’ is equal to:

sum(x*df[1:i,1])+sum(y*df[1:i,2])

**Exercise 4**

Create a function that given a numeric vector, sort this in ascending order and duplicate it by two.

**Exercise 5**

Create a function that given a vector alpha numeric, keep only the numbers and apply the function created on exercise 4.

For example, if the input is a vector `w="a" "v" "7" "4" "q"`

, the function will return `w=8 14`

.

**Exercise 6**

Create a function that given a string

ST='NAME: Maria /COUNTRY:uruguay /EMAIL: mariaUY@gmail.com'

return a matrix

[,1] [,2]

[1,] "NAME" " Maria "

[2,] "COUNTRY" "uruguay "

[3,] "EMAIL" " mariaUY@gmail.com"

**Exercise 7**

Consider a vector:

ST=c('NAME:Maria /COUNTRY:uruguay /EMAIL:mariaUY@gmail.com','NAME:Paul/COUNTRY:UK /EMAIL:PaulUK@gmail.com',

'NAME:Jhon /COUNTRY:USA /EMAIL:JhonUSA@gmail.com','NAME:Carlos /COUNTRY:Spain /EMAIL:CarlosSP@gmail.com')

Create a function that given a vector string ST return a matrix:

[,1] [,2] [,3] [,4] [,5]

[1,] "NAME" "Maria " "Paul" "Jhon " "Carlos "

[2,] "COUNTRY" "uruguay " "UK " "USA " "Spain "

[3,] "EMAIL" "mariaUY@gmail.com" "PaulUK@gmail.com" "JhonUSA@gmail.com" "CarlosSP@gmail.com"

**Exercise 8**

Create a function that given a numeric vector X returns the digits 0 to 9 that are not in X. If `X=0 2 4 8 `

the function return `1 3 5 6 7 9`

**Exercise 9**

Create a function that given two strings (one word each), check if one is an anagram of another.

**Exercise 10**

Create a function that given one word, return the position of word’s letters on `letters`

vector.

For example, if the word is ‘abc’, the function will return 1 2 3.

**Want to practice functions a bit more? We have more exercise sets on this topic here.**

carl sutton says

not having any experience with regex some of problems exceeded any attempt to solve. shrugged and had a nip of single malt and copied the answer.

\\”p\\””sorkges'[gt5pdrsil no idea what that means