Home » R Tutorials » Indexing into Data Frames in R

Indexing into Data Frames in R

Indexing in R starts at 1, which means the first element is at index 1. In R indexing data frame can be achieved using the brackets notation to access the elements. Data frame is like a two-dimensional array or table where rows are the observations and the columns are the variable.

If you want to access the element of the 2nd row and 1 column, use [2,1] indexing in the data frame to access the value of the element.

In this tutorial, we will discuss indexing in data frames, indexing with name, boolean indexing into a data frame in R.

Indexing into Data Frames in R

Using the square bracket notation, you can access the element, or data can be extracted from the data frame.

Let’s practice with an example to understand how indexing data frames in R works.

Create a data frame using data.frame() function.

# Create a data frame
student_info <- data.frame(
  name = c("Tom","Kim","Sam","Julie","Emily","Chris"),
  age = c(20,21,19,20,21,22),
  gender = c('M','F','M','F','F','M'),
  marks = c(72,77,65,80,85,87)
)
# Print data frame
student_info

The output of the above data frame is:

   name age gender marks
1   Tom  20      M    72
2   Kim  21      F    77
3   Sam  19      M    65
4 Julie  20      F    80
5 Emily  21      F    85
6 Chris  22      M    87

Get an element at row 2 and column 4,

student_info[2,4]

It returns the marks scored by the Kim student.

[1] 77

Get an element at row 3 and column name as age

student_info[3,"age"]
19

Get the first two rows and all columns of data

student_info[1:2,]
  name age gender marks
1  Tom  20      M    72
2  Kim  21      F    77

Get the first three rows with specific columns as name, marks

student_info[1:3,c("name","marks")]
  name marks
1  Tom    72
2  Kim    77
3  Sam    65

Indexing with Boolean in Data Frame

Let’s consider the above data frame to indexing into boolean for the data frame.

Get the boolean vector for students who scores greater than 80 marks.

student_info$marks > 80

The output of the above R code is a boolean vector having either TRUE or FALSE value.

The output result shows that the last two students in the data frame have scored more than 80 marks.

[1] FALSE FALSE FALSE FALSE  TRUE  TRUE

Get resultant data frame for the students who have scored more than 80 marks.

student_info[student_info$marks >80,]

The output of the above R code is:

   name age gender marks
5 Emily  21      F    85
6 Chris  22      M    87

Conclusion

I hope the above article on R indexing into the data frame is helpful to you.

Use square bracket notation to get an element from the data frame. You can extract the data from the data frame using the column name in indexing into the data frame.

Leave a Comment