为了编写一个 R 程序来创建长度为6的数值、复数、逻辑和字符类型的向量,我们使用内置函数 vector()。通过在 R 中使用此函数,我们可以创建任何类型的向量。此函数的语法是
vector(mode = "logical", length = 0)
其中,
在此 R 程序中,我们将向量值接受到变量 N、C、L、Chr 中。变量 N 用于保存数值向量,C 用于复数向量,L 用于逻辑向量,最后 Chr 用于保存字符向量。这些向量中的每一个都是通过调用 vector() 函数并将其类型和长度作为参数创建的。最后,每个向量都作为结果打印出来。
步骤 1:获取变量 N、C、L、Chr 以保存不同类型的向量。
步骤 2:首先创建数值向量 N,例如 N= vector("numeric", 5)
步骤 3:打印向量 N
步骤 4:创建复数向量 C,例如 C= vector("complex", 5)
步骤 5:打印向量 C
步骤 6:创建逻辑向量 L,例如 L= vector("logical", 5)
步骤 7:打印向量 L
步骤 8:创建字符向量 Chr,例如 Chr= vector("character", 5)
步骤 9:打印向量 Chr
N= vector("numeric", 5)
print("Numeric Type:")
print(N)
C= vector("complex", 5)
print("Complex Type:")
print(C)
L= vector("logical", 5)
print("Logical Type:")
print(L)
Chr= vector("character", 5)
print("Character Type:")
print(Chr)
[1] "Numeric Type:" [1] 0 0 0 0 0 [1] "Complex Type:" [1] 0+0i 0+0i 0+0i 0+0i 0+0i [1] "Logical Type:" [1] FALSE FALSE FALSE FALSE FALSE [1] "Character Type:" [1] "" "" "" "" ""