📌  相关文章
📜  如何按列的内容对 R DataFrame 进行排序?

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

如何按列的内容对 R DataFrame 进行排序?

在本文中,我们将讨论如何在 R 编程语言中按列的内容对 DataFrame 进行排序。我们可以使用 order()函数。带有提供的参数的 order()函数返回一个排列,将其第一个参数重新排列为升序或降序,通过进一步的参数打破联系。

示例 1:



R
gfg_data <- data.frame(
  Country = c("France","Spain","Germany","Spain","Germany",
              "France","Spain","France","Germany","France"),
   
  age = c(44,27,30,38,40,35,52,48,45,37),
   
  salary = c(6000,5000,7000,4000,8000),
   
  Purchased=c("No","Yes","No","No","Yes","Yes","No","Yes",
              "No","Yes")
)
 
gfg_data[order(gfg_data$Country),]


R
gfg_data <- data.frame(
  Country = c("France","Spain","Germany","Spain","Germany",
              "France","Spain","France","Germany","France"),
     
  age = c(44,27,30,38,40,35,52,48,45,37),
     
  salary = c(6000,5000,7000,4000,8000),
     
  Purchased=c("No","Yes","No","No","Yes","Yes","No","Yes",
              "No","Yes")
)
 
gfg_data[order(gfg_data$age),]


输出:

r 排序数据帧 1

示例2:

电阻

gfg_data <- data.frame(
  Country = c("France","Spain","Germany","Spain","Germany",
              "France","Spain","France","Germany","France"),
     
  age = c(44,27,30,38,40,35,52,48,45,37),
     
  salary = c(6000,5000,7000,4000,8000),
     
  Purchased=c("No","Yes","No","No","Yes","Yes","No","Yes",
              "No","Yes")
)
 
gfg_data[order(gfg_data$age),]

输出:

R排序数据框2