从 R 中的向量中删除多个值
在本文中,我们将讨论如何在 R 编程语言中从向量中删除多个值。
我们将使用 %in%运算符删除多个值
句法:
vector <- vector[! vector %in% c(elements)]
在哪里,
- 向量是输入向量
- 元素是要删除的值
- %in%运算符检查向量元素是否存在于实际向量中
示例 1:从向量中删除多个值的 R 程序
R
# create a vector
a=c(1,2,"Sravan",4,5,"Bobby",4,5,6,"Ojaswi","Rohith",56.0)
# display a
print(a)
# Remove multiple values
a <- a[! a %in% c("Sravan",4,6, "Rohith")]
print("---------")
# display a
print(a)
R
# create a vector
a=c(1,2,"Sravan",4,5,"Bobby",4,5,6,"Ojaswi","Rohith",56.0)
# display a
print(a)
# Remove multiple values
a <- a[! a %in% c("Sravan",1,2)]
print("---------")
# display a
print(a)
输出:
[1] “1” “2” “Sravan” “4” “5” “Bobby” “4” “5”
[9] “6” “Ojaswi” “Rohith” “56”
[1] “———“
[1] “1” “2” “5” “Bobby” “5” “Ojaswi” “56”
示例 2:从向量中删除多个值的 R 程序
电阻
# create a vector
a=c(1,2,"Sravan",4,5,"Bobby",4,5,6,"Ojaswi","Rohith",56.0)
# display a
print(a)
# Remove multiple values
a <- a[! a %in% c("Sravan",1,2)]
print("---------")
# display a
print(a)
输出:
[1] “1” “2” “Sravan” “4” “5” “Bobby” “4” “5”
[9] “6” “Ojaswi” “Rohith” “56”
[1] “———“
[1] “4” “5” “Bobby” “4” “5” “6” “Ojaswi” “Rohith”
[9] “56”