R의 열을 기준으로 데이터 프레임 정렬

Sheeraz Gul 2023년6월21일 R R Function
  1. order()를 사용하여 R에서 열별로 데이터 프레임 정렬
  2. arrange()를 사용하여 R에서 열별로 데이터 프레임 정렬
  3. set.order()를 사용하여 R에서 열별로 데이터 프레임 정렬
R의 열을 기준으로 데이터 프레임 정렬

R에서 열을 기준으로 데이터 프레임을 정렬하는 방법은 여러 가지가 있습니다. 이 자습서에서는 R에서 다양한 방법을 사용하여 열을 기준으로 데이터 프레임을 정렬하는 방법을 보여줍니다.

order()를 사용하여 R에서 열별로 데이터 프레임 정렬

order() 메서드는 오름차순 또는 내림차순으로 열별로 데이터 프레임을 정렬할 수 있습니다. order() 메서드는 2개의 매개변수를 사용합니다. 열 이름이 있는 데이터 프레임과 True 또는 False가 될 수 있는 두 번째는 감소합니다.

감소가 True이면 데이터 프레임이 내림차순으로 정렬되고 False이면 데이터 프레임이 오름차순으로 정렬됩니다.

코드 예:

employee_data = data.frame(
  employeeId = c(10, 15, 14, 12, 13),
  salary = c(3000, 2500, 1000, 3500, 2000))

print(employee_data)

print("sorting in decreasing order based on employee id ")
print(employee_data[order(employee_data$employeeId, decreasing = TRUE), ] )

print("sorting in increasing order based on salary ")
print(employee_data[order(employee_data$salary, decreasing = FALSE), ] )

위의 코드는 order()를 사용하여 employeeIdsalary 열을 기준으로 데이터 프레임을 정렬했습니다.

출력:

  employeeId salary
1         10   3000
2         15   2500
3         14   1000
4         12   3500
5         13   2000

[1] "sorting in decreasing order based on employee id "
  employeeId salary
2         15   2500
3         14   1000
5         13   2000
4         12   3500
1         10   3000

[1] "sorting in increasing order based on salary "
  employeeId salary
3         14   1000
5         13   2000
2         15   2500
1         10   3000
4         12   3500

arrange()를 사용하여 R에서 열별로 데이터 프레임 정렬

arrange() 메서드는 dplyr 라이브러리의 함수입니다. 이 방법은 오름차순으로 열별로 데이터 프레임을 정렬합니다.

첫 번째는 데이터 프레임이고 두 번째는 열 이름입니다. dplyr 패키지가 설치되어 있지 않으면 먼저 패키지를 설치해야 합니다.

install.packages("dplyr")

코드 예:

library("dplyr")

employee_data = data.frame(
  employeeId = c(10, 15, 14, 12, 13),
  salary = c(3000, 2500, 1000, 3500, 2000))

print(employee_data)

print("sorting in increasing order based on employee id ")
print(arrange(employee_data, employeeId))

print("sorting in increasing order based on salary ")
print(arrange(employee_data, salary))

위의 코드는 오름차순으로 두 열을 기준으로 데이터 프레임을 정렬합니다.

출력:

  employeeId salary
1         10   3000
2         15   2500
3         14   1000
4         12   3500
5         13   2000

[1] "sorting in increasing order based on employee id "
  employeeId salary
1         10   3000
2         12   3500
3         13   2000
4         14   1000
5         15   2500

[1] "sorting in increasing order based on salary "
  employeeId salary
1         14   1000
2         13   2000
3         15   2500
4         10   3000
5         12   3500

set.order()를 사용하여 R에서 열별로 데이터 프레임 정렬

set.order()는 열을 기준으로 데이터 프레임을 오름차순으로 정렬할 수 있는 데이터 테이블 패키지의 메서드입니다. arrange() 메소드와 유사한 매개변수를 사용합니다.

코드 예:

library("data.table")

employee_data = data.frame(
  employeeId = c(10, 15, 14, 12, 13),
  salary = c(3000, 2500, 1000, 3500, 2000))

print(employee_data)

print("sorting in increasing order based on employee id ")
print(setorder(employee_data, employeeId))

print("sorting in increasing order based on salary ")
print(setorder(employee_data, salary))

위의 코드는 employeeIdsalary 열을 기준으로 오름차순으로 데이터 프레임을 정렬합니다.

출력:

  employeeId salary
1         10   3000
2         15   2500
3         14   1000
4         12   3500
5         13   2000

[1] "sorting in increasing order based on employee id "
  employeeId salary
1         10   3000
4         12   3500
5         13   2000
3         14   1000
2         15   2500

[1] "sorting in increasing order based on salary "
  employeeId salary
3         14   1000
5         13   2000
2         15   2500
1         10   3000
4         12   3500
튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
작가: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

관련 문장 - R Function