Wednesday, March 31, 2021

Selecting and Displaying Parts of a Vector in R Language

 


   Selecting and Displaying Parts of a Vector

 

·     *   Being able to select and display the parts of a vector is one of the most important reasons of selection of a Vector .

 

·     * If one has a large sample of data , then in case if one wants to obtain a large sample of data , then one may want to see which of the items are larger than which of the values which would require the user of the data to select those data that are larger ones among the dataset

 

·    *   In an alternative scenario , one may want to extract a series of values as a subsample from an analysis .

 

·    *    Being able to select / extract required parts of a vector is one of the most important aspects of performing many more complicated operations in R tool .

 

* The various examples or processes that one may come across while doing any type of selection of a chunk of data from a vector are in form of given scenarios :

 

·        extraction of the first item / single item from within a vector ;
·        selection of the third item ( nth item) from within a vector ;
·        selection of the first to the third items from a vector ;
·        selection and extraction of all items from a vector ;
·        selection of items from the combination vector ;
·        selection of all items which are greater than the value 3 (that means selection and extraction of given items with a value for the number greater than or lesser than some particular number) ;
·        Showing items which are either greater than or lesser than some set of numbers








  The other useful commands over the objects which can be used to extract the various parts of data are : length() command which can be used to find the length of a given vector .

·        The length command can be also put to use to obtain / extract segments of data from square brackets :

 

data[ length(data) - 5 : length(data)]

 

In the above given scenario example the last five elements of the vector are found out from the above used code :

·        

·        max() command can be used to get the largest value in the vector

 

==========================================================

> data1

[1] 4 6 8 6 4 3 7 9 6 7 10

 

> max( data1 )

[1] 10

 

> which( data1 == max(data1))

[1] 11

==========================================================

 

# The upper command -- "which" is showing the index number or position of the largest data from within the data vector . The maximum value of all the data elements present within the "data1" vector is 10 . The positional index value of the data from the vector is 11 .

 

·        The first command .. max() provides the actual value which is the largest value within the vector and the second command asks which of the elements is the largest .

 

·        Another useful command is one that generates sequences from a vector which can be expressed in the form .. seq()

·        While using the "sequence" vector , one may need to pick out the beginning to ending of the interval vectors . In given words , one may select the first , third , fifth and so on vectors using the given in sequence parameters like the start , end and the interval values .

 

·        Therefore putting the full scale general form of the "sequence" command can be writen in the given form :

 

seq(start ,end ,interval)

 

 

·        The above command will work on character vectors as well as numeric vectors in the given manner :

 ==========================================================

> data5

   "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct"     "Nov" "Dec"

 

> data5[-1:-6]

   "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" 

=================================================

* In the above code , the last 6 strings which are actually the three letter initials of each of the months of a calendar year are found out as result .

 

No comments:

Post a Comment