w3resource

Order the airquality Data Frame by Columns in R Programming

R Programming: Data frame Exercise-25 with Solution

Write a R program to call the (built-in) dataset airquality. Check whether it is a data frame or not? Order the entire data frame by the first and second column.

Sample Solution :

R Programming Code :

# Load the built-in airquality dataset
data = airquality

# Print a description of the dataset
print("Original data: Daily air quality measurements in New York, May to September 1973.")

# Print the class of the dataset to confirm it's a data frame
print(class(data))

# Print the first 10 rows of the dataset
print(head(data,10))

# Order the entire data frame by the first column (Ozone) and then by the second column (Solar.R)
result = data[order(data[,1], data[,2]),]

# Print a message indicating that the data frame is ordered by the first and second columns
print("Order the entire data frame by the first and second column:")

# Print the ordered data frame
print(result)

Output:

[1] "Original data: Daily air quality measurements in New York, May to September 1973."
[1] "data.frame"
   Ozone Solar.R Wind Temp Month Day
1     41     190  7.4   67     5   1
2     36     118  8.0   72     5   2
3     12     149 12.6   74     5   3
4     18     313 11.5   62     5   4
5     NA      NA 14.3   56     5   5
6     28      NA 14.9   66     5   6
7     23     299  8.6   65     5   7
8     19      99 13.8   59     5   8
9      8      19 20.1   61     5   9
10    NA     194  8.6   69     5  10
[1] "Order the entire data frame by the first and second column:"
    Ozone Solar.R Wind Temp Month Day
21      1       8  9.7   59     5  21
23      4      25  9.7   61     5  23
18      6      78 18.4   57     5  18
...........
119    NA     153  5.7   88     8  27
150    NA     145 13.2   77     9  27                         

Explanation:

  • # Load the built-in airquality dataset
    • Loads the built-in airquality dataset into the variable data.
  • data = airquality
    • Assigns the airquality dataset to the variable data.
  • # Print a description of the dataset
    • Prints a message describing the dataset.
  • print("Original data: Daily air quality measurements in New York, May to September 1973.")
    • Displays the message about the dataset being the daily air quality measurements.
  • # Print the class of the dataset to confirm it's a data frame
    • Checks and prints the class of the data object to confirm it is a data frame.
  • print(class(data))
    • Outputs the class of the data object, which should be data.frame.
  • # Print the first 10 rows of the dataset
    • Prints the first 10 rows of the data data frame.
  • print(head(data,10))
    • Displays the first 10 rows of the data data frame.
  • # Order the entire data frame by the first column (Ozone) and then by the second column (Solar.R)
    • Orders the data frame by the Ozone column and then by the Solar.R column.
  • result = data[order(data[,1], data[,2]),]
    • Creates a new data frame result where the rows are ordered first by the Ozone column and then by the Solar.R column.
  • # Print a message indicating that the data frame is ordered by the first and second columns
    • Prints a message indicating that the data frame has been ordered.
  • print("Order the entire data frame by the first and second column:")
    • Displays the message about ordering the data frame.
  • # Print the ordered data frame
    • Prints the result data frame to show the ordered output.
  • print(result)
    • Displays the ordered result data frame.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to create a data frame using two given vectors and display the duplicated elements and unique rows of the said data frame.
Next: Write a R program to call the (built-in) dataset airquality. Remove the variables 'Solar.R' and 'Wind' and display the data frame.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/r-programming-exercises/dataframe/r-programming-data-frame-exercise-25.php