官术网_书友最值得收藏!

How to do it…

Creating a vector in R is the most easy task.

Take a look at the following steps to learn how to create a vector in R:

  1. There are several ways in which you can create a vector, but the most popular and easy way to create the vector is using the concatenate function c() as shown in the following code snippet:
        cVec <- c("Cricket", "Football", "Basketball", "Rugby")
  1. You can store the vector by giving a name of the object; in this case, cVec is the object name that contains the character vector. The elements of this vector do not have any name, but you can give them a name. If the elements of a vector have a name, then it is called a "named vector". For example, you can give the name of the elements of the vector cVec as follows:
        cVec <- c(game1="Cricket", game2="Football",        
game3="Basketball", game4="Rugby")
  1. Execute the following code to create numeric and logical vectors using the c() function:
        nVec <- c(1:10)
Lvec <- c(TRUE, FALSE, FALSE, TRUE)
  1. Though c() is the most easy and convenient way to create a vector in R, you can create a vector by generating the number or character or logical sequence using any other facilities in R, such as, creating random numbers within certain intervals, say 10 to 20, or taking random sample of letters from the set of alphabets or the output of any conditional operations. Here are some examples:
        nVec2 <- runif( n=5, min=10, max=20)  #to generate random numbers 
from Uniform distribution
cVec2 <- sample(x=letters, size= 5, replace = F)
Lvec2 <- nVec2>=13
  1. After creating a vector, the next step is to verify its type, that is, the data type of the vector that you have just created. To access the properties of the vector, you can use one of the following functions:
    • The is.character() function
    • The is.numeric() function
    • The is.integer() function
    • The is.logical() function
  2. However, before accessing the data types, the first thing you must do is verify that the object that you have created is a vector using the is.vector() function. The output of this function will generate a true/false:
        is.vector(cVec)
is.vector(nVec)
is.vector(Lvec)
is.vector(cVec2)
is.vector(nVec2)
is.vector(Lvec2)

The number of elements in a vector could be as few as one, to as many as you want depending on the computer’s architecture and memory. Usually, the maximum length for a vector in R for all builds could be 2^31-1, but for a 64-bit build, it could be 2^34-1.

  1. After creating a vector, you might need to access a single element of the vector, and the position of that single element could be anywhere in the vector. In R, the element of the vector can be accessed by using the index number of the vector. The index ranges from 1 to the length of the vector. To access the specific element of a vector, the index needs to be supplied within a square bracket []. Here is an example of accessing the third element of the cVec vector:
        cVec[3]
  1. You can access any number of elements from the vector by providing the index of the element inside the square bracket. In other words, to get access to multiple elements of a vector, you have to provide the index as another vector inside the square brackets as follows:
        cVec[c(1,3,5)]
主站蜘蛛池模板: 长沙市| 嵊泗县| 扬中市| 昌图县| 兴仁县| 平乡县| 凌源市| 通州市| 浦北县| 九龙县| 思南县| 洛隆县| 桃园市| 夏河县| 贵定县| 灵宝市| 嘉兴市| 巴林右旗| 绥芬河市| 滦平县| 会宁县| 凤城市| 黔西| 炉霍县| 宝坻区| 岳池县| 衡阳县| 高雄市| 灵川县| 长岭县| 枝江市| 涿鹿县| 固始县| 城固县| 调兵山市| 吉隆县| 武宁县| 正安县| 长白| 京山县| 南昌县|