📜  如何反转 R 中给定向量的顺序?

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

如何反转 R 中给定向量的顺序?

在本文中,我们将讨论如何使用 R 编程语言中的rev()函数反转向量中元素的顺序。

句法:

方法

  • 创建一个向量
  • 反转元素

创建向量的语法:



示例:创建向量

R
# create vector with names
name=c("sravan","mohan","sudheer","radha","vani","mohan")
  
# create vector with subjects
subjects=c(".net","Python","java","dbms","os","dbms")
  
# create a vector with marks
marks=c(98,97,89,90,87,90)
  
# create vector with height
height=c(5.97,6.11,5.89,5.45,5.78,6.0)
  
# create vector with weight
weight=c(67,65,78,65,81,76)
  
# pass these vectors to the vector
data=c(name,subjects,marks,height,weight)
  
# display
print(data)


R
print("reverse")
print(rev(name))
print(rev(subjects))
print(rev(marks))
print(rev(height))
print(rev(weight))


输出:

示例:反转元素的顺序

电阻

print("reverse")
print(rev(name))
print(rev(subjects))
print(rev(marks))
print(rev(height))
print(rev(weight))

输出:

向量 r 的逆序