R – 创建空向量并附加值
在本文中,我们将讨论如何在 R 编程语言中创建一个空向量并将元素添加到向量中。可以通过在使用c()函数创建常规向量时不传递任何值来创建空向量。
句法:
c()
这将返回 NULL 作为输出。
例子:
R
# create an empty vector a
a=c()
# display it
print(a)
R
# create an empty nested
# vector a
a=c(c(),c())
# display it
print(a)
R
# create an empty vector a
a=c()
# display it
print(a)
# adding numbers from 1 to
# 20 to a vector
a=1:20
# display a
print(a)
R
# create an empty vector a
a=c()
# display it
print(a)
# adding names to vector which
# is empty
a=c('sravan','bobby','rohith','gnnaesh','gajji')
# display a
print(a)
R
# create an empty numeric
# vector a
a=c()
# display it
print(a)
# create an empty numeric
# vector b
b=c()
# display it
print(b)
# create an empty numeric
# vector d
d=c()
# display it
print(d)
# include numeric data into
# vector a insert value 10
# at location 1
a[1]=10
# insert value 20 at location 2
a[2]=20
# insert value 14.5 at location 3
a[3]=14.5
# insert value 89.000 at location 4
a[4]=89.000
# display vector a
print(a)
# include logical data into vector
# b at locations 1,2,3
b[1]=TRUE
b[2]=FALSE
b[3]=FALSE
# display vector b
print(b)
# include character data into vector
# d at locations 1,2,3
d[1]="Sravan"
d[2]="Bobby"
d[3]="pinkey"
# display vector
print(d)
R
# create an empty numeric
# vector a
a=c()
# display it
print(a)
# include all type of vector
# data into vector a
a[1]="sravan"
a[2]=20
a[3]=14.5
a[4]=FALSE
# display vector a
print(a)
R
# create an empty numeric
# vector a
a=c()
# display it
print(a)
# append 10 using append()
# function
a=append(a,10)
# display
print(a)
R
# create an empty numeric
# vector a
a=c()
# display it
print(a)
# append 10 elements from 1 to
# 10 using append() function
a=append(a,c(1:10))
# display
print(a)
输出:
NULL
也可以用 R 编程语言创建嵌套的空向量。
例子:
电阻
# create an empty nested
# vector a
a=c(c(),c())
# display it
print(a)
输出:
NULL
向空向量添加值
方法 1:使用范围
我们可以使用 range (:)运算符将元素添加到空向量
语法:
start_value:end_value
例子:
电阻
# create an empty vector a
a=c()
# display it
print(a)
# adding numbers from 1 to
# 20 to a vector
a=1:20
# display a
print(a)
输出:
NULL
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
方法 2:使用另一个向量
为此,首先创建一个空向量,然后为其分配一个向量。
例子:
电阻
# create an empty vector a
a=c()
# display it
print(a)
# adding names to vector which
# is empty
a=c('sravan','bobby','rohith','gnnaesh','gajji')
# display a
print(a)
输出:
NULL
[1] "sravan" "bobby" "rohith" "gnnaesh" "gajji"
方法三:使用索引
我们可以使用“[]”运算符(称为索引运算符)在空向量中分配/填充值
语法:
vector_name[index_location]=data
其中,vector_name 是创建的空向量的名称
- Index_location 是特定元素所在的索引值
- 数据是分配给特定索引位置的值
示例 1:
电阻
# create an empty numeric
# vector a
a=c()
# display it
print(a)
# create an empty numeric
# vector b
b=c()
# display it
print(b)
# create an empty numeric
# vector d
d=c()
# display it
print(d)
# include numeric data into
# vector a insert value 10
# at location 1
a[1]=10
# insert value 20 at location 2
a[2]=20
# insert value 14.5 at location 3
a[3]=14.5
# insert value 89.000 at location 4
a[4]=89.000
# display vector a
print(a)
# include logical data into vector
# b at locations 1,2,3
b[1]=TRUE
b[2]=FALSE
b[3]=FALSE
# display vector b
print(b)
# include character data into vector
# d at locations 1,2,3
d[1]="Sravan"
d[2]="Bobby"
d[3]="pinkey"
# display vector
print(d)
输出:
NULL
NULL
NULL
[1] 10.0 20.0 14.5 89.0
[1] TRUE FALSE FALSE
[1] "Sravan" "Bobby" "pinkey"
我们可以在一个空向量中插入所有类型的向量。
示例 2:
电阻
# create an empty numeric
# vector a
a=c()
# display it
print(a)
# include all type of vector
# data into vector a
a[1]="sravan"
a[2]=20
a[3]=14.5
a[4]=FALSE
# display vector a
print(a)
输出:
NULL
[1] "sravan" "20" "14.5" "FALSE"
方法 4:使用 append()
我们可以使用 append()函数添加数据。
句法:
append(vector_name,value)
其中,vector_name 是向量的名称,value 是输入值。
例子:
电阻
# create an empty numeric
# vector a
a=c()
# display it
print(a)
# append 10 using append()
# function
a=append(a,10)
# display
print(a)
输出:
NULL
[1] 10
我们还可以使用 c()函数附加多个数据
句法:
append(vector,c(value1,value2,.value n))
例子:
电阻
# create an empty numeric
# vector a
a=c()
# display it
print(a)
# append 10 elements from 1 to
# 10 using append() function
a=append(a,c(1:10))
# display
print(a)
输出:
NULL
[1] 1 2 3 4 5 6 7 8 9 10