Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Sorting Data | Data Manipulation and Cleaning
Data Analysis with R

bookSorting Data

メニューを表示するにはスワイプしてください

Sorting is a fundamental operation in data analysis. It allows you to organize your dataset based on one or more variables - such as price, mileage, or year. This makes it easier to identify trends, outliers, or simply view the data in a meaningful order.

Sorting in Ascending Order

Base R

You can use the order() function to sort a dataset by column values. By default, this returns the data in ascending order.

df_sorted_price_base <- df[order(df$selling_price), ]

dplyr

Sorting can be done using the arrange() function, which also defaults to ascending order.

df_sorted_price_dplyr <- df %>%
  arrange(selling_price)

Sorting in Descending Order

Base R

To sort in descending order, place a negative sign (-) in front of the column inside the order() function.

df_sorted_price_desc <- df[order(-df$selling_price), ]

dplyr

You can use the desc() function inside arrange() to reverse the order.

sorted_price_desc_dplyr <- df %>%
  arrange(desc(selling_price))

Sorting by Multiple Columns

It is possible to sort by more than one column to create a prioritized order. For example, you might sort first by fuel type (alphabetically) and then by selling price in descending order.

Base R

df_sorted <- df[order(df$fuel, -df$selling_price), ]

dplyr

df_sorted_dplyr <- df %>%
  arrange(fuel, desc(selling_price))
question mark

What does order(df$selling_price) do?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  8

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  8
some-alt