Branches of mechanical engineering: Functions Exercises + Solution - Vol. Ii - R



[For this exercise, showtime write downward your answer, without using R. Then, cheque your reply using R.]
Answers to the exercises are available here.
Exercise 1
Create a business office that given a information frame as well as a vector, volition add together a the vector (if the vector length tally amongst the rows pose out of the information frame)
as a novel variable to the information frame.
Exercise 2
Consider a information 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 business office that, given a information frame as well as 2 indexes, exchanges 2 values ​​of the Code variable amongst each other.
For example, if the index is 1 as well as 3, you lot assign:

df[1,'Code']=df[3,'Code']
df[3,'Code']=df[1,'Code']
Exercise 3
Consider 2 variables x,y as well as a information 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 business office that given a information frame df calculate a novel variable ‘SUM_x_y'(If x=2 as well as y=3, thus the novel variable volition travel ‘SUM_2_3’,
if x=4 as well as y=10, thus the novel variable volition travel ‘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 business office that given a numeric vector, variety this inwards ascending lodge as well as duplicate it past times two.
Exercise 5
Create a business office that given a vector alpha numeric, proceed exclusively the numbers as well as utilize the business office created on job 4.
For example, if the input is a vector w="a" "v" "7" "4" "q" , the business office volition return w=8 14.
Exercise 6
Create a business office 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 business office that given a vector string ST provide 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 business office that given a numeric vector X returns the digits 0 to nine that are non inwards X. If X=0 2 4 8 
the business office return 1 3 v vi seven 9
Exercise 9
Create a business office that given 2 strings (one give-and-take each), cheque if i is an anagram of another.
Exercise 10
Create a business office that given i word, provide the seat of word’s letters on letters vector.
For example, if the give-and-take is ‘abc’, the business office volition provide 1 2 3.

Want to practise functions a fleck more? We accept to a greater extent than job sets on this topic here.


________________________________________________________

Below are the solutions to these exercises.
#################### #                  # #    Exercise 1    # #                  # #################### # Create a business office that given a information frame as well as a vector, volition add together a the vector (if the vector length tally amongst the rows pose out of the information frame) # equally a novel variable to the information frame.   Id=rep(c(1,2,3,4),each=3) Letter=rep(letters[1:3],4) x=seq(1,43,along.with=Id) y=seq(-20,0,along.with=Id) M=data.frame(Id,Letter,x,y)  ADD_COL=function(M,v) {   if(nrow(M)==length(v))     M=cbind(M,v)   return(M) }  z=seq(1,100,along.with=Id) M=ADD_COL(M,z)  u=c(1:10) M=ADD_COL(M,u)   w=seq(0,20,along.with=Id) M=ADD_COL(M,w)  M 
##    Id Letter         x          y   v         v ## 1   1      a  1.000000 -20.000000   1  0.000000 ## 2   1      b  4.818182 -18.181818  10  1.818182 ## 3   1      c  8.636364 -16.363636  xix  3.636364 ## 4   2      a 12.454545 -14.545455  28  5.454545 ## v   2      b 16.272727 -12.727273  37  7.272727 ## vi   2      c 20.090909 -10.909091  46  9.090909 ## seven   3      a 23.909091  -9.090909  55 10.909091 ## 8   3      b 27.727273  -7.272727  64 12.727273 ## nine   3      c 31.545455  -5.454545  73 14.545455 ## 10  4      a 35.363636  -3.636364  82 16.363636 ## xi  4      b 39.181818  -1.818182  91 18.181818 ## 12  4      c 43.000000   0.000000 100 20.000000 
#################### #                  # #    Exercise 2    # #                  # #################### #Consider a information 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 business office that, given a information frame as well as 2 indexes, exchanges 2 values of the Code variable amongst each other.  #For example, if the index is 1 as well as 3, you lot assign:#df[1,'Code']=df[3,'Code'] #df[3,'Code']=df[1,'Code']  change=function(df,a,b) {   aux=df[a,'Code']   df[a,'Code']=df[b,'Code']   df[b,'Code']=aux   return(df) }  df=change(df,1,3) df=change(df,7,2) df=change(df,5,10) df 
##    Id Age Sex Code ## 1   1  xiv   F    c ## 2   2  12   chiliad    g ## 3   3  xv   chiliad    a ## 4   4  10   F    d ## v   v  23   chiliad    j ## vi   vi  21   F    f ## seven   seven  41   chiliad    b ## 8   8  56   chiliad    h ## nine   nine  78   F    i ## 10 10  12   chiliad    e 
#################### #                  # #    Exercise 3    # #                  # #################### # Consider 2 variables x,y as well as a information 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 business office that given a information frame df calculate a novel variable 'SUM_x_y'(If x=2 as well as y=3, thus the novel variable volition travel 'SUM_2_3',  # if x=4 as well as y=10, thus the novel variable volition travel 'SUM_4_10'),such that for each row 'i' is: #  sum(x*df[1:i,1])+sum(y*df[1:i,2])  A=c(1:10) B=seq(100,10,-10) H=seq(-200,-50,along.with=B) df=data.frame(A,B,H)  NEWDF=function(df,x,y) {   for (i in 1:nrow(df))     df[i,4]=sum(x*df[1:i,1])+sum(y*df[1:i,2]) names(df)[4]=paste('SUM',x,y,sep='_') df }  NEWDF(df,3,6) 
##     Influenza A virus subtype H5N1   B          H SUM_3_6 ## 1   1 100 -200.00000     603 ## 2   2  ninety -183.33333    1149 ## 3   3  fourscore -166.66667    1638 ## 4   4  seventy -150.00000    2070 ## v   v  threescore -133.33333    2445 ## vi   vi  l -116.66667    2763 ## seven   seven  xl -100.00000    3024 ## 8   8  thirty  -83.33333    3228 ## nine   nine  xx  -66.66667    3375 ## 10 10  10  -50.00000    3465 
NEWDF(df,7,5) 
##     Influenza A virus subtype H5N1   B          H SUM_7_5 ## 1   1 100 -200.00000     507 ## 2   2  ninety -183.33333     971 ## 3   3  fourscore -166.66667    1392 ## 4   4  seventy -150.00000    1770 ## v   v  threescore -133.33333    2105 ## vi   vi  l -116.66667    2397 ## seven   seven  xl -100.00000    2646 ## 8   8  thirty  -83.33333    2852 ## nine   nine  xx  -66.66667    3015 ## 10 10  10  -50.00000    3135 
#################### #                  # #    Exercise 4    # #                  # #################### #Create a business office that given a numeric vector, variety this inwards ascending lodge as well as duplicate it past times two.  FUNVector1=function(v) {   v=sort(v)*2   return(v) } v=c(2,4,1,7,3,2,7,9) FUNVector1(v) 
## [1]  2  4  4  vi  8 xiv 14 eighteen 
#################### #                  # #    Exercise v    # #                  # #################### # Create a business office that given a vector alpha numeric, proceed exclusively the numbers, as well as utilize the business office created on job 4. # For example, if the input is a vector <code>w="a" "v" "7" "4" "q"</code> , the business office volition provide <code>w=8 14</code>.  FUNVector2=function(w) {   w=as.numeric(w[-which(w %in% letters)])   w   w=FUNVector1(w)   return(w) } a=c('a','v',4,7,'q') a=FUNVector2(a) a 
## [1]  8 xiv 
b=c(letters[1:23],10:1,letters[24:26],11:22) b=FUNVector2(b) b 
##  [1]  2  4  vi  8 10 12 xiv sixteen eighteen xx 22 24 26 28 thirty 32 34 36 38 xl 42 44 
#################### #                  # #    Exercise vi    # #                  # #################### # Create a business office that given a string  # ST='NAME: Maria /COUNTRY:uruguay /EMAIL: mariaUY@gmail.com' # provide a data.frame #      [,1]      [,2]               # [1,] "NAME"    " Maria "          # [2,] "COUNTRY" "uruguay "         # [3,] "EMAIL"   " mariaUY@gmail.com" #  ST='NAME: Maria /COUNTRY:uruguay /EMAIL: mariaUY@gmail.com' DF=function(ST) { A=unlist(strsplit(ST,'/')) M=rbind(unlist(strsplit(A[1],':')),unlist(strsplit(A[2],':')),unlist(strsplit(A[3],':'))) return(M) } M=DF(ST) M 
##      [,1]      [,2]                 ## [1,] "NAME"    " Maria "            ## [2,] "COUNTRY" "uruguay "           ## [3,] "EMAIL"   " mariaUY@gmail.com" 
#################### #                  # #    Exercise seven    # #                  # #################### # 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 business office that given a vector string ST provide 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"  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') DF2=function(V) {   M=DF(V[1])   for (i in 2:length(V))     M=cbind(M,DF(V[i])[,2])   return(M) } DF2(ST) 
##      [,1]      [,2]                [,3]               [,4]                ## [1,] "NAME"    "Maria "            "Paul"             "Jhon "             ## [2,] "COUNTRY" "uruguay "          "UK "              "USA "              ## [3,] "EMAIL"   "mariaUY@gmail.com" "PaulUK@gmail.com" "JhonUSA@gmail.com" ##      [,5]                 ## [1,] "Carlos "            ## [2,] "Spain "             ## [3,] "CarlosSP@gmail.com" 
#################### #                  # #    Exercise 8    # #                  # #################### #Create a business office that given a numeric vector X returns the digits 0 to nine that are non inwards X  DIGITS=function(X) {   b=0:9   Y=b[!b%in%a]   return(Y) } a=c(1,5,3,8) DIGITS(a) 
## [1] 0 2 4 vi seven nine 
#################### #                  # #    Exercise nine    # #                  # #################### #Create a business office that given 2 strings, cheque if i is an anagram of another. library('stringr') 
library('stringi') 
ANAGRAM=function(a,b) { x=unlist(stri_extract_all(a, regex=c('\\p{L}'))) y=unlist(stri_extract_all(b, regex=c('\\p{L}'))) if (length(x)==length(y))   {cond=unique(x%in%y==y%in%x)    cat('anagram:',ifelse(length(x)==length(y) & length(cond)==1,ifelse(cond==TRUE,TRUE,FALSE),FALSE)) } if (length(x)!=length(y))   cat('anagram: FALSE') } a='serpent' b='present' ANAGRAM(a,b) 
## anagram: TRUE 
x='married' y='admirer' ANAGRAM(x,y) 
## anagram: TRUE 
x='Deduction' y='Discounted' ANAGRAM(x,y) 
## anagram: FALSE 
#################### #                  # #    Exercise 10   # #                  # #################### #Create a business office that given i word, provide the seat of word's letters on letters vector.  ORDERWORD=function(a) { x=unlist(stri_extract_all(a, regex=c('\\p{L}'))) POSITION=which(letters%in%x) return(POSITION) } a='hello' ORDERWORD(a) 
## [1]  v  8 12 15
 
 
Source:
http://www.r-exercises.com/2016/12/05/functions-exercises-vol-2/
http://www.r-exercises.com/2016/12/05/functions-vol-2-solutions/

Sumber http://engdashboard.blogspot.com/

Jangan sampai ketinggalan postingan-postingan terbaik dari Branches of mechanical engineering: Functions Exercises + Solution - Vol. Ii - R. Berlangganan melalui email sekarang juga:

Bali Attractions

BACA JUGA LAINNYA:

Bali Attractions