Wednesday, 10 January 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.)


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 Feedback/Comments/Query.

Subscribe to Blog Posts by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts