Indexing or accessing the Data Frame in R

By indexing the Data Frame in R we will get the particular column data. Indexing can be done by specifying column names in square brackets. The syntax for indexing the data frame is-

dataframeName[“columnName”]

R




# create a data frame 
stats <- data.frame(player=c('A', 'B', 'C', 'D'),
                runs=c(100, 200, 408, NA),
                wickets=c(17, 20, NA, 5))
 
print("stats Dataframe")
stats
 
# fetch data in certain column
stats["runs"]


Output:

  player runs wickets
1 A 100 17
2 B 200 20
3 C 408 NA
4 D NA 5
stats["runs"]
runs
1 100
2 200
3 408
4 NA

In this example we create a Data Frame “stats” that contains runs scored and wickets taken by a player and perform indexing on the data frame to extract runs scored by players.

Indexing and Slicing Data Frames in R

In this article let’s discuss indexing and slicing the Data Frames or how to access elements of a data frame in R Programming Language.

Similar Reads

What is Indexing or accessing?

The process of accessing particular data components or subsets within a vector, matrix, or data frame is called indexing. It enables us to pick, remove, or change particular values or parts of our data based on criteria....

Indexing or accessing the Data Frame in R

By indexing the Data Frame in R we will get the particular column data. Indexing can be done by specifying column names in square brackets. The syntax for indexing the data frame is-...

Slicing In R

...

Slicing or accessing the Data Frame

The process of extracting or accessing particular subsets or sections of a vector, matrix, or data frame depending on predetermined criteria is known as slicing. Using start and end indices, we can choose sequential ranges of elements or subsets of data....