Blog

R Practice Questions covering 'String Processing' as well as 'Regular Expression' -

Question 1 :
------------------
      a) Define a String Vector as c("Data Science" ,"Science of data")
      b) Detect whether "data" is present in the above string Vector

Question 2 :
-----------------
     a) Use "state.name" dataset available with base R 
     b) Load this dataset into your R Enviroonment    [data("state")]
     c) Now , display the State names in the descending order of the length of the State name

Question 3 :
-----------------
     a) Use "state.name" dataset available with base R 
     b) How many states are having "u" or "v" or "w" or "x" or "y" or "z" in their name ( only show count)  
         ( matching should be case insensitive)
     c) Show the positions of the countries meeting the condition in question b
     d) Show all the states meeting the condition in above question b.
     e) Show the count of the states whose name is having more than one word
     f) Display only those states whose name is having more than one word 
     g) take the list of state names from previous question f and create an output containing the individual words of the State names 
         which contains more than 1 word in them
     h) given with Direction_names <- c( "north" , "south" , "east" , "west")
        create a regex string which finds those states which contains any of the individual string given in
        Direction_names String Vector in their name at the beginning .

Question 4 :
-----------------
     Using Regular Expression -
      a) Detect whether a string is having Leading or Trailing Space(s)
      b) Remove only the Leading & / or trailing Spaces in a String 

     Without using regex
      c) Remove the Leading / Trailing Spaces in a string using the functions from stringr package
      d) Remove all whitespaces present anywhere within string ( embedded whitespaces to be replaced with single blank space)

Question 5 :
---------------
   When four friends were asked about their Date of Birth , following responses were received .....
    as stored in the below mentioned Char Vector
         Ans_Birth <- c( "I was born on year 2000", 
                          "1985 is the year I was born" , 
                          "My date of birth is 22 June 1988"  , 
                          "My Year of birth was 1990 & month of birth Feb")

   And the name of the four friends are stored in following vector
         Friends_Vec <- c( "David" , "Rahim" , "Julie" , "Raam")

      a) extract the Year part only from the four answers received
      b) Create a Tibble dataset have 2 columns 
                  Column 1 : Name of the four Friends
                  Column 2 : Year of Birth extracted from the Answers

Question 6 :
-----------------
     Validate using the regular expression that the date provided are valid .
      Where the Valid Date meets the following conditions
        a) valid date format is -
             first 2 numbers are Day number, 
             numbers in 4th & 5th position are Month number
                   numbers in 7th thru 10th position is year number
        b) Valid date format only contains following characters as delimiter
                 between Day , Month & Year -
              "/" , "." , "-" 
        c) Dates given as input to be validated are 
           c("21/11/2000","31/13/2000","31-12-2018","01.01.19","31,12,2018")
           Assuming that the same delimiter is used within a date

Question 7 :
-----------------
For the following given Input String .....

a) Input_Str_7 <- "String 34 contains 88 numbers 55 which 56 are 99 repeated 12"
    Extract only those 2 digit numbers whose both digits are same ( ex 88)  

b) Input_Str_7_2 <- "String 212 having 234 three 22 digit 808 numbers 111 , 332"
    Extract only those 3 digit numbers whose first & last digit are same. ( Ex - 212 ) 


Question 8 :
-----------------
We know that list.file() returns all the files in your working directory .

Form this file list generated by list.files() command , only show those file names which ends with ".csv"

Thanks
Priyaranjan Mohanty
@AUTHOR : Admin

Tags:Eco, Water, Air, Environment

Comments (0)

    No Comments Found
Leave a Comment