Wednesday, January 10, 2018

What is Vector in R Programming and how to Add and Delete Vector Elements

How to Add and Delete Vector Elements in R Programming
Vectors are one-dimensional arrays that can hold numeric data, character data, or logical
data. The combine function c() or colon : is used to form the vector.
Example:
a <- c(1, 2, 5, 3, 6, -2, 4)
b <- c("one", "two", "three")
c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)
Here, a is a numeric vector, b is a character vector, and c is a logical vector. Note that
the data in a vector must be only one type or mode (numeric, character, or logical).
You can’t mix modes in the same vector.

Note : Scalars are one-element vectors. Examples include f <- 3, g <- "US",and h <- TRUE. They’re used to hold constants.
You can refer to elements of a vector using a numeric vector of positions within brackets.
For example, a[c(2, 4)] refers to the second and fourth elements of vector a.
Here are additional examples:
> a <- c("k", "j", "h", "a", "c", "m")
> a[3]
[1] "h"
> a[c(1, 3, 5)]
[1] "k" "h" "c"
> a[2:6]
[1] "j" "h" "a" "c" "m"
The colon operator used in the last statement generates a sequence of numbers. 
For example, a <- c(2:6) is equivalent to a <- c(2, 3, 4, 5, 6).
The Vector is a fundamental data type in R.In many programming languages, vector variables are considered different from scalars, which are single-number variables. Consider the following C code, for example:
int x;
int y[3];
This requests the compiler to allocate space for a single integer named x and a three-element integer array (C terminology analogous to R’s vector type) named y
But in R, numbers are actually considered one-element vectors,and there is really no such thing as a scalar.
R variable types are called modes. All elements in a vector must have the same mode, which can be integer, numeric (floating-point number), character (string), logical (Boolean), complex,and so on. If you need your program code to check the mode of a variable x,
you can query it by the call typeof(x).
Unlike vector indices in ALGOL-family languages, such as C and Python,vector indices in R begin at 1.
Adding Vector Elements:
Vectors are stored like arrays in C, contiguously, and thus you cannot insert or delete the elements—something you may be used to if you are a Python programmer.
The size of a vector is determined at its creation, so if you wish to add or delete elements, you’ll need to reassign the vector.
For example, let’s add an element to the middle of a four-element vector:
> x <- c(64,5,12,45)
> x <- c(x[1:2],75,x[3],x[4]) # insert 75  before the 12
> x
[1] 64 5 75 12 45
Here, we created a four-element vector and assigned it to x. To insert a new number 75 between the second and third elements, we string together the first two elements of x, then the 75, then the fourth and fifth elements of x with concatenation function c(). This creates a new five-element vector, leaving x intact for the time being.
We then assigned that new vector to x.In the result, it appears as if we had actually changed the vector stored in x, but really we created a new vector and stored that vector in x. This difference may seem subtle, but it has implications.
Deleting Vector Elements:
we will just re-assign the vector to delete and update the vector elements.
> x <- c(64,75,12,45)
> x<- c(75,12)
> x
[1] 75  12
> x<- c(x[3:5])
> x
[1] 75  12  45.
Obtaining the Length of a Vector:
We can obtain the length of a vector by using the length() function. It returns the no.of elements in a vector.
> x <- c(1,3,5,4,6,7)
> length(x)
[1]6
> x <- c()
> x
NULL
> length(x)
[1] 0
> 1:length(x)
[1] 1 0
Vector Variable Declaration and value assignment to Elements:
Typically, compiled languages require that you declare variables; that is, warn the interpreter /compiler of the variables’ existence before using them. 
This is the case in our earlier C example:
int x;
int y[3];
As with most scripting languages (such as Python and Perl), you do not declare variables in R. For instance, consider this code:
z <- 3
This code, with no previous reference to z, is perfectly legal (and commonplace).
However, if you reference specific elements of a vector, you must warn R. For instance, say we wish y to be a two-component vector with values 5 and 12. 
The following will not work:
> y[1] <- 5
> y[2] <- 12
Instead, you must create y first, for instance this way:
> y <- vector(length=2)
> y[1] <- 5
> y[2] <- 12
The following will also work:
> y <- c(5,12)
This approach is all right because on the right-hand side we are creating a new vector, to which we then bind y.
The reason we cannot suddenly spring an expression like y[2] on R stems from R’s functional language nature. The reading and writing of individual vector elements are actually handled by functions. If R doesn’t already know that y is a vector, these functions have nothing on which to act.
Speaking of binding, just as variables are not declared, they are not constrained in terms of mode. The following sequence of events is perfectly valid:
> x <- c(1,5)
> x
[1] 1 5
> x <- "abc"
First, x is associated with a numeric vector, then with a string. (Again, for C/C++ programmers : x is nothing more than a pointer, which can point to different types of objects at different times.)
Generating various Vectors :
>x<-c(5,9,3,10,7,2,8)
> x
[1]  5  9  3 10  7  2  8

#reversing the vector elements
> rev(x)

[1]  8  2  7 10  3  9  5

#sorting the vector elements, default is ascending order.
> sort(x)
[1]  2  3  5  7  8  9 10
> sort(x,decreasing =T)
[1] 10  9  8  7  5  3  2
#repeating the vector elements
> rep(0,100)
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> rep(1:3,6)
[1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
Notice also a variation on the use of this function
> rep(1:3,c(6,6,6))
[1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3
which we could also simplify cleverly as
> rep(1:3,rep(6,3))
[1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3

#generating a vector with Month names and Month abbreviations.
> month_names<-month.name
> month_names
 [1] "January"   "February"  "March"     "April"     "May"       "June"      "July"     

"August"    "September" "October"   "November"  "December"
> month_abbr<-month.abb
> month_abbr
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
> sort(month_abbr)
 [1] "Apr" "Aug" "Dec" "Feb" "Jan" "Jul" "Jun" "Mar" "May" "Nov" "Oct" "Sep"
#generating a vector sample letters.
> K<-sample(LETTERS,10)
> K
 [1] "N" "F" "Y" "S" "R" "V" "P" "A" "Z" "G"
> j<-sample(letters,10)
> j
 [1] "g" "r" "p" "e" "h" "z" "n" "i" "s" "l"
#generating a vector with random numbers
> ru<-runif(10,min=5,max=15)
> ru
 [1]  7.615625 12.687843  8.938393 14.468555 11.966720  5.958154 11.047052  8.825901 10.483278  5.293615
> rn<-rnorm(25,mean=15,sd=5)
> rn
 [1] 18.948690 11.033506 13.198424 11.140057 21.499868  9.130856 18.288699 21.187561 21.719519 20.708266 20.140673  6.177530  6.094785  9.356247 24.065943
[16] 24.734930 19.383492 14.864321 25.230627 14.831957 13.887351 13.883249 18.299107 11.611315 20.920300

#Copying a Vector to another Vector the remove the original one
> x<-LETTERS[1:5]
> x
[1] "A" "B" "C" "D" "E"

> {y<-x;rm(x)}
> y
[1] "A" "B" "C" "D" "E"

Important Note :
When we use colon : to form a vector with numerical data, the data will be stored in the type of integer. If we use alone the combine c() function to form a vector with numerical data, the data will be stored in the type of double.
> x<-c(1:5)
> x
[1] 1 2 3 4 5
> mode(x)
[1] "numeric"
> class(x)
[1] "integer"
> typeof(x)
[1] "integer"
> x<-c(1,2,3,4,5)
> x
[1] 1 2 3 4 5
> mode(x)
[1] "numeric"
> class(x)
[1] "numeric"
> typeof(x)
[1] "double"

--------------------------------------------------------------------------------------------------------
Thanks, TAMATAM ; Business Intelligence & Analytics Professional
--------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.