How To Change Values In A Vector In R
R Vector
In this article, you'll learn about vector in R programming. Yous'll learn to create them, access their elements using unlike methods, and modify them in your program.
Vector is a bones data construction in R. It contains element of the aforementioned type. The data types tin be logical, integer, double, character, complex or raw.
A vector's blazon can be checked with the typeof()
function.
Another of import property of a vector is its length. This is the number of elements in the vector and can be checked with the function length()
.
How to Create Vector in R?
Vectors are generally created using the c()
role.
Since, a vector must take elements of the same blazon, this part will try and coerce elements to the same type, if they are different.
Coercion is from lower to college types from logical to integer to double to character.
> x <- c(1, 5, 4, nine, 0) > typeof(x) [one] "double" > length(ten) [i] 5 > x <- c(1, 5.four, TRUE, "hello") > 10 [1] "1" "5.iv" "True" "hello" > typeof(x) [1] "graphic symbol"
If we desire to create a vector of consecutive numbers, the :
operator is very helpful.
Example one: Creating a vector using : operator
> x <- 1:7; x [1] ane 2 iii iv v 6 vii > y <- two:-2; y [i] ii i 0 -1 -2
More circuitous sequences can be created using the seq()
function, like defining number of points in an interval, or the stride size.
Example ii: Creating a vector using seq() function
> seq(one, 3, by=0.ii) # specify step size [1] one.0 1.2 1.4 ane.6 ane.8 2.0 two.two ii.four 2.half dozen 2.8 3.0 > seq(1, 5, length.out=4) # specify length of the vector [1] 1.000000 2.333333 3.666667 5.000000
How to access Elements of a Vector?
Elements of a vector can be accessed using vector indexing. The vector used for indexing can be logical, integer or character vector.
Using integer vector as index
Vector index in R starts from 1, dissimilar most programming languages where index offset from 0.
We can use a vector of integers every bit index to access specific elements.
We can also utilise negative integers to return all elements except that those specified.
Simply we cannot mix positive and negative integers while indexing and real numbers, if used, are truncated to integers.
> ten [1] 0 2 iv half dozen 8 10 > 10[3] # admission tertiary element [ane] 4 > x[c(ii, four)] # admission 2nd and 4th element [1] two vi > ten[-1] # admission all merely 1st element [one] ii 4 half-dozen viii 10 > 10[c(ii, -iv)] # cannot mix positive and negative integers Error in x[c(2, -four)] : only 0's may be mixed with negative subscripts > x[c(2.4, iii.54)] # real numbers are truncated to integers [1] 2 4
Using logical vector equally index
When we use a logical vector for indexing, the position where the logical vector is TRUE
is returned.
This useful feature helps us in filtering of vector as shown below.
> x[c(Truthful, FALSE, Simulated, TRUE)] [1] -three 3 > x[x < 0] # filtering vectors based on conditions [one] -3 -1 > x[x > 0] [i] 3
In the above example, the expression x>0
will yield a logical vector (Simulated, Faux, FALSE, True)
which is and so used for indexing.
Using graphic symbol vector as alphabetize
This type of indexing is useful when dealing with named vectors. Nosotros can proper name each elements of a vector.
> x <- c("first"=three, "second"=0, "third"=9) > names(x) [1] "kickoff" "second" "third" > ten["2nd"] second 0 > x[c("start", "third")] start third iii 9
How to modify a vector in R?
We tin can modify a vector using the consignment operator.
We can use the techniques discussed to a higher place to access specific elements and modify them.
If nosotros want to truncate the elements, we can use reassignments.
> x [1] -3 -two -1 0 one ii > x[2] <- 0; 10 # modify 2nd element [ane] -three 0 -1 0 i 2 > x[10<0] <- 5; ten # modify elements less than 0 [1] 5 0 5 0 1 2 > ten <- x[one:four]; x # truncate x to first 4 elements [ane] five 0 5 0
How to delete a Vector?
We can delete a vector by simply assigning a Naught
to information technology.
> ten [i] -3 -two -ane 0 ane 2 > ten <- Null > x Nix > ten[4] Zilch
Source: https://www.datamentor.io/r-programming/vector/
Posted by: xiongbefornes.blogspot.com
0 Response to "How To Change Values In A Vector In R"
Post a Comment