In this Data Analysis tutorial we will show you how to extract and display the first (or first N) rows of your R DataFrame.
Create an example DataFrame
#create R vectors
x <- c(seq (11,15))
y <- c (x ^ 2)
z <- c (ppois (x,8))
#Initialize DataFrame
test <- data.frame (x = x, y = y, z = z)
Here’s our DataFrame:
x | y | z | |
---|---|---|---|
1 | 11 | 121 | 0.8880760 |
2 | 12 | 144 | 0.9362028 |
3 | 13 | 169 | 0.9658193 |
4 | 14 | 196 | 0.9827430 |
5 | 15 | 225 | 0.9917690 |
Get the first n rows in R
Using bracket notations
n=1 # adjust as needed
test[n,]
Remember: You can subset an R DataFrame by using the following syntax: your_df[rows, columns]. If you have a background in Python and pandas, that might be confusing. Make sure to add a comma following the row position, otherwise you’ll be selecting R DataFrame columns not rows.
Using the head function
n=1 # adjust as needed
head(test,n)
Both methods will render the following result:
x y z 1 11 121 0.888076
Using dplyr to get first n rows
We will now go ahead and explain how to use dplyr slice function to subset our DataFrame rows. In this example we’ll slice the first two rows of our data.
library(dplyr)
n=2 # adjust as needed
test %>% slice(seq(n))
Alternatively:
library(dplyr)
n=2
first_rows = slice(test,seq(n))
print (first_rows)
Both will render this DataFrame:
x y z 1 11 121 0.8880760 2 12 144 0.9362028
Converting the first row to a vector
Once extracted, we can easily convert the DataFrame first row to a vector.
n=1
test_vec <- as.numeric(head(test,n))
print (test_vec)
This will render an R vector:
[1] 11.000000 121.000000 0.888076
Get the top N rows according to criteria
So far, we used the row position to govern which rows to extract. But using the dplyr top_n function we can sort our DataFrame and slice the topn n rows according to any of our DataFrame columns:
library(dplyr)
n=2
top_n(test,n,z)