Вы находитесь на странице: 1из 22

Learn

 R  By  Intensive  Practice

Final Coding Test


http://www.machinelearningplus.com

facebook.com/rtipaday  
@R_Programming
Q1. R as a calculator
Exercise  1:  Calculate  square  root  of  729      

Exercise  2:  Compute  the  remainder  when  1203  


is  divided  by  22  

Exercise  3:  Calculate  the  number  of  days  


between  ‘4th  October  2013’  to  ‘3rd  November  
2015’
Q2. Creating a numeric
variable
Exercise  1:  Create  a  new  variable  'b'  with  value  
1947.0      

Exercise  2:  Check  the  class  of  the  new  variable  


‘b’  

Exercise  3:  Check  the  memory  size  of  the  ‘b’


Q3. Convert a numeric
variable to character
Exercise  1:  Convert  'b'  from  previous  exercise  
to  character  and  store  it  in  a  new  variable  ‘a’.  

Exercise  2:  Check  the  memory  size  of  ‘a’.  Is  it  
different  from  that  of  ‘b’?  

   
Q4. Setting up working
directory
Exercise  1:  Find  out  your  current  working  
directory.  

Exercise  2:  Setup  your  working  directory  to  a  


new  'work'  folder  in  your  desktop      
Q5. Create an environment
within the global environment
Exercise  5.1:  Create  a  new  environment  called  
'myEnv'      

Exercise  5.2:  Create  a  variable  ‘b’  with  value  of  3  


inside  it      

Exercise  5.3:  Access  the  variable  ‘b’  inside  the  new  


environment      

Exercise  5.4:  Delete  the  variable  inside  that  


environment    
Q6. Create a numeric vector
Exercise  6.1:  Create  a  vector  of  numbers  from  1  
to  6  and  find  out  its  class      

Exercise  6.2:  Create  a  vector  containing  


following  mixed  elements  {1,  'a',  2,  'b'}  and  
find  out  its  class      
Q7. Initialise Character vector

Exercise  7.a:  Initialise  a  character  vector  of  


length  26      

Exercise  7.b:  Assign  the  character  'a'  to  the  


first  element  in  above  vector.    
Q8. Vector operations
Exercise  8.1:  Create  a  vector  with  some  of  your  friend's  names      

Exercise  8.2:  Get  the  length  of  above  vector      

Exercise  8.3:  Get  the  first  two  friends  from  above  vector      

Exercise  8.4:  Get  the  2nd  and  3rd  friends      

Exercise  8.5:  Sort  your  friends  by  names  in  ascending,  using  2  
methods      

Exercise  8.6:  Reverse  direction  of  the  above  sort  


Q9. Create customised vector
sequences
Exercise  9.1:  Create  the  following  sequence  
with  rep  and  seq  functions.    

                                       {‘a’,  ‘a’,  1,  2,  3,  4,  5,  7,  9,  11}    
Q10. Remove missing values

Exercise  10:  Remove  missing  values  using    


2  methods.  

     myVec  <-­‐  c(1,  2,  NA,  4,  5,  6,  7,  NA  ,  10)
Q11. Random sampling
Exercise  11.1:  Pick  50  random  numbers  
between  1  to  180,  with  replacement  and  store  
it  in  vec1  

Exercise  11.2:  Pick  25  percent  of  numbers  


between  1  to  180,  without  replacement  and  
store  in  vec2
Q12. Set Operations
Exercise  12.1:  Get  the  duplicated  items  in  vec1,  
created  in  previous  exercise.  

Exercise  12.2:  Get  the  common  items  in  vec1  


and  vec2.
Q13. Data.Frame
Implement  the  following  tasks  on  the  iris  dataset.  

Exercise  13.1:  Find  the  class  of  ‘iris’  

Exercise  13.2:  Find  the  class  of  all  the  columns  of  ‘iris’  

Exercise  13.3:  Get  the  summary  of  ‘iris’  

Exercise  13.4:  Get  the  top  6  rows  

Exercise  13.5:  View  it  in  a  spreadsheet  format  

Exercise  13.6:  Get  row  names  

Exercise  13.7:  Get  column  names  

Exercise  13.8:  Get  number  of  rows  

Exercise  13.9:  Get  number  of  columns


Q14. Subsetting a dataframe
Exercise  14.1:  Get  the  last  2  rows  in  last  2  
columns  from  iris  dataset      

Exercise  14.2:  Get  rows  with  Sepal.Width  >  3.5  


using  which()  from  iris      

Exercise  14.3:  Get  the  rows  with  'versicolor'  


species  using  subset()  from  iris    
Q15. Joining Data.Frames
Preparatory  Code  
set.seed(100)  

Df1  <-­‐  iris[sample(1:nrow(iris),  10),  c(1,2,3,5)]  

Df2  <-­‐  iris[sample(1:nrow(iris),  10),  c(1,2,4,5)  ]  

Exercise  15.1:  Do  inner  join,  outer  join,  left  join  


and  right  join  of  Df1  and  Df2.    

Merge  using  the  by  variable  as  'Species'      


Q16. Create customised character
vector using the paste function
Exercise  16:  Create  following  character  vector  
using  paste:    

'var1',  'var2',  'var3',  'pred1',  'pred2',  'pred3'    


Q17. Creating Frequency
Tables and Aggregations
Exercise  17.1:  Tabulate  the  counts  of  each  
Species  from  iris      

Exercise  17.2:  What  is  the  mean  of  Petal.Width  


for  each  species  of  iris?
Q18. Create a logic using a
For-loop
Problem  Logic  for  Q18,  19  and  20:  Create  a  character  vector  with  
length  of  number  of  rows  of  iris-­‐dataset,  such  that,  each  element  
gets  a  character  value  –  “greater  than  5″  if  the  corresponding  
‘Sepal.Length’  >  5,  else  it  should  get  “lesser  than  5″.  

Exercise  18:  Make  the  logic  for  above  problem  


statement  using  a  for-­‐loop
Q19. Create a logic using a
ifelse() function
Problem  Logic  for  Q18,  19  and  20:  Create  a  character  vector  with  
length  of  number  of  rows  of  iris-­‐dataset,  such  that,  each  element  
gets  a  character  value  –  “greater  than  5″  if  the  corresponding  
‘Sepal.Length’  >  5,  else  it  should  get  “lesser  than  5″.  

Exercise  19:  Create  a  logic  for  the  same  


problem  statement  using  ifelse()  function
Q20. Create a logic using
apply()
Problem  Logic  for  Q18,  19  and  20:  Create  a  character  vector  with  
length  of  number  of  rows  of  iris-­‐dataset,  such  that,  each  element  
gets  a  character  value  –  “greater  than  5″  if  the  corresponding  
‘Sepal.Length’  >  5,  else  it  should  get  “lesser  than  5″.  

Exercise  20:  Create  a  logic  for  the  same  


problem  statement  using  apply()  function
Solutions
Visit  the  following  url  for  solutions  

http://bit.ly/2mXwMkV

Вам также может понравиться