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

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

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

在本文中,我们将讨论如何在 R 编程语言中反转向量中元素的顺序。

可以使用rev()函数来完成。它返回数据对象的反向版本。

示例 1:这里我们将创建一个向量并使用 rev()函数将其反转。

R
# create vector with names
vec = c("sravan", "mohan", "sudheer",
         "radha", "vani", "mohan")
  
print("Original vector-1:")
print(vec)
rv = rev(vec)
  
print("The said vector in reverse order:")
print(rv)


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("Original vector-1:")
print(data)
rv = rev(data)
  
print("The said vector in reverse order:")
print(rv)


输出:



示例 2:这里我们将创建多个向量,然后将它们反转。

电阻

# 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("Original vector-1:")
print(data)
rv = rev(data)
  
print("The said vector in reverse order:")
print(rv)

输出: