Functions exercises vol. 2
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
[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: [email protected]'
return a matrix
[,1] [,2]
[1,] "NAME" " Maria "
[2,] "COUNTRY" "uruguay "
[3,] "EMAIL" " [email protected]"
Exercise 7
Consider a vector:
ST=c('NAME:Maria /COUNTRY:uruguay /EMAIL:[email protected]','NAME:Paul/COUNTRY:UK /EMAIL:[email protected]',
'NAME:Jhon /COUNTRY:USA /EMAIL:[email protected]','NAME:Carlos /COUNTRY:Spain /EMAIL:[email protected]')
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" "[email protected]" "[email protected]" "[email protected]" "[email protected]"
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.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.