![]() |
VOOZH | about |
A data set typically means a collection of data organized in a tabular form, like a spreadsheet or a database table. Various programming languages have different techniques/methods to represent a data set in C++ we use vectors or arrays of structs/objects, In python pandas Data Frames, and Java 2D arrays or collections, similarly in R Programming Language we have built-in data structures called data frames which is similar to pandas Data Frames in python.
Given below is the representation of data frames in R
Output:
S.No. Name Age Score
1 1 Ram 19 95
2 2 Rohan 20 85
3 3 Aman 18 80
4 4 Rahul 21 90
5 5 Ravi 20 88
So for all the above reasons the extraction can be used. Now we will study various methods of row extraction from top and bottom.
head() and tail() functions in R used to extract the first and last few rows of a dataset/data frame. Given below is the syntax of head() and tail() function.
Output:
S.No. Name Age Score
1 1 Ram 19 95 S.No. Name Age Score
5 5 Ravi 20 88
where,
Now let us apply these functions to a particular data set.
Output:
ID Item Price BestBefore
1 1 oil 50 12
2 2 cream 80 6 ID Item Price BestBefore
4 4 soap 30 24
5 5 gel 100 12
As you can see from the above code for n=2 the first and last 2 rows are printed.
Here are the syntax for the index slicing
my_dataFrame[ a:b , c:d ]Here a, b refers to start and end of the range of rows to be extracted and c, d for columns.
Now let's use it to extract 1st row of some particular data frame
Output:
Name Age Score
1 Alice 20 95 ID Item Price BestBefore
4 4 soap 30 24
5 5 gel 100 12
As you can see we used the same square bracket technique just the difference is that here we first found the total rows of the data frame and then used it to extract the last 2 rows.
slice_head() and slice_tail() are used to extract subset of rows from a dataframe from start and end respectively. So this makes it an another one of the methods to extract first/last rows of the dataset.
let us code it and print first and last rows of any given data frame.
Output:
Language Articles AverageRating
1 Python 1200 4.8 Language Articles AverageRating
1 R 300 4
subset() as the name indicates allows you to take out a particular subset from a data set based on some conditions. Its syntax is as follows:
subset(x, rows_to_select, columns_to_select, drop = FALSE)Output:
Geek_ID Geek_Name Age
1 1 Prim 26 Geek_ID Geek_Name Age
5 5 Dijkstra 25
So these all were various methods to print the first or last rows of a data set, depending upon the use case and users conveniency any of them can be used.