📜  在 R 编程中分配向量

📅  最后修改于: 2022-05-13 01:55:39.920000             🧑  作者: Mango

在 R 编程中分配向量

向量是R中最基本的数据结构之一。它们包含相同类型的数据。 R 中的向量等价于其他编程语言中的数组。在 R 中,数组是一个或多个维度的向量,创建的每个对象都以向量的形式存储。向量的成员被称为组件。

R中的向量

向量的分配

有多种分配向量的方法。在 R 中,可以使用c()或使用“:”或使用seq()函数来执行此任务。

  • 使用c()分配向量
    通常,R 中的向量是使用c()函数分配的。示例 1:

    # R program to illustrate
    # Assigning vectors
      
    # Using c()
    V = c(1, 2, 4, 6, 7)
      
    # Printing the Vector
    print(V)
      
    # Printing the data type of the vector
    print(typeof(V))
    

    输出

    [1] 1 2 4 6 7
    [1] "double"
    

    示例 2:

    # R program to illustrate
    # Assigning vectors
      
    # by default numeric values,
    # double values and logical values
    # are converted into characters 
    V2 = c(1.5, TRUE, 4, "Geeks")
      
    # Printing the Vector
    print(V2)
      
    # Printing the data type of the vector
    print(typeof(V2))
    

    输出

    [1] "1.5"   "TRUE"  "4"     "Geeks"
    [1] "character"
    
  • 使用“:”分配向量
    在 R 中,要创建连续值的向量,使用“:”运算符。
    示例 1:

    # R program to illustrate
    # Assigning vectors
      
    # use':' to assign a vector  
    # of continuous values
    V = 1:10
      
    # Printing the vector
    print(V)
    

    输出

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

    示例 2:

    # R program to illustrate
    # Assigning vectors
      
    # use':' to assign a vector  
    # of continuous values
    V = 1.5 : 9.5
      
    # Printing the vector
    print(V)
    

    输出

    [1] 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5
    

    示例 3:

    如果间隔不匹配,它会跳过最后一个值。

    # R program to illustrate
    # Assigning vectors
      
    # use':' to assign a vector  
    # of continuous values
    # instead of 9.5
    # here we take 9.4
    V = 1.5 : 9.4
      
    # Printing the vector
    print(V)
    

    输出

    [1] 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 
    
  • 使用seq()分配向量
    为了创建具有步长的向量,R 提供了seq()函数。
    示例 1:

    # R program to illustrate
    # Assigning vectors
      
    # Assigning a vector using
    # seq() function 
    V = seq(1, 3, by=0.2)
      
    # Printing the vector
    print(V)
    

    输出

    [1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0
    

    示例 2:

    可以指定所需的向量长度,并自动计算步长。

    # R program to illustrate
    # Assigning vectors
      
    # Creating a vector using seq()
    # specifying the length of the vector
    V = seq(1, 10, length.out=5)    
      
    # Printing the vector
    print(V)
    

    输出

    [1]  1.00  3.25  5.50  7.75 10.00
    

在 R 中分配命名向量

也可以在 R 中创建命名向量,以便为每个值分配一个名称。 R 提供了names()函数来创建命名向量。

例子:

假设一个人想要创建一个命名向量,其中包含每项运动的运动员人数。为此,首先,他将创建一个包含玩家数量的数字向量。现在,他可以使用names()函数将运动的名称分配给运动员人数。

# R program to illustrate
# Assigning named vectors
  
# Creating a numeric vector
# with the number of players
sports.players = c(2, 4, 5, 6, 7, 9, 11)
  
# Assigning sports name to the numeric vector
names(sports.players) = c("Bridge", "Polo", "Basketball", 
                          "Volleyball", "kabaddi", 
                          "Baseball", "Cricket")
  
# Displaying the named vector
print(sports.players)

输出

Bridge   Polo   Basketball  Volleyball  kabaddi   Baseball    Cricket 
  
    2      4            5           6        7          9         11 

为了获得具有特定数量玩家的运动:

# Displaying the sports with 9 players
print(names(sports.players[sports.players==9]))
  
# Displaying the sports with 1 player
print(names(sports.players[sports.players==1]))

输出:

"Baseball"
character(0)

说明

棒球有九名球员,因此它将棒球显示为输出。由于在此命名向量中没有一个运动员的运动,因此不会生成输出,并将输出显示为字符(0)。

访问向量的元素

在 R 中,为了访问向量的元素,可以执行向量索引。

示例 1:

# R program 
# To access elements
  
# Creating a vector by seq() function
V = seq(1, 40, by= 4)
  
# Printing the vector
print(V)
  
# Printing the fifth element of the vector
print(V[5])

输出:

[1] 1  5  9 13 17 21 25 29 33 37
[1] 17

示例 2:

# R program 
# To access multiple elements
  
# Creating a vector by seq() function
V = seq(1, 40, by= 4)
  
# Printing the vector
print(V)
  
# Printing the fifth and seventh element of the vector
print(V[c(5,7)])

输出:

[1] 1  5  9 13 17 21 25 29 33 37
[1] 17 25