Access Data of R Data Frame

R Tutorial – Learn to Access Data of R Data Frame like selecting rows, selecting columns, selecting rows that have a given column value, etc., with Example R Scripts.

Access Data of R Data Frame

R Data Frame is 2-Dimensional table like structure. In a dataframe, row represents a record while columns represent properties of the record.

We shall look into following items to access meta information and data of an R Data Frame :

We shall use an R data frame for examples with columns : name, age, income.

Structure of R Data Frame

Following is the R function used to extract structure of an R Data Frame :

str(<data_frame_name>)

Example R Script to extract structure of an R Data Frame :

 

Summary of R Data Frame

Following is the R function used to extract summary of an R Data Frame

summary(<data_frame_name>)

Example R Script to extract summary of an R Data Frame :

 

Get element of R Data Frame

Following is the R function used to extract element from ith row, jth column of an R Data Frame

<data_frame_name>[c(<comma,separated,row,numbers>),c(<comma,separated,column,numbers>)]

Example R Script to extract element from ith row, jth column of an R Data Frame :

 

Extract column(s) of Data Frame

Following is the R function used to extract some of the columns from a R Data Frame.

data.frame(<data_frame_name>$<column_name_1>,<data_frame_name>$<column_name_2>)

You may select one or more columns from a data frame. If you are selecting multiple columns, use a comma separated list. Please observe that to select a column, we use <data_frame_name> followed by $ symbol followed by <column_name_1>.

You may write the result to a new Data Frame or overwrite the original data frame.

Example R Script to extract columns (age, income) of R Data Frame (celebrities):

 

Add row(s) to R Data Frame

Following R function is used to add more rows to an R Data Frame :

resulting_data_frame = rbind(<existing_data_frame_name>,<additional_data_frame_name>)

Following is an Example R Script that adds three rows to the existing R data frame, celebrities :

 

Add column(s) to R Data Frame

Following R function is used to add more columns to an R Data Frame :

resulting_data_frame = rbind(<existing_data_frame_name>,<new_data_frame_name>)

Following is an Example R Script that add a column, ‘car’ to the existing R data frame, celebrities :

 

Delete column(s) of R Data Frame

To delete a column from R Data Frame, you may select the columns you want to keep using extract columns of data frame and overwrite the existing data frame.

 

Conclusion :

In this R Tutorial, we have learnt to Access Data of R Data Frame like selecting rows, selecting columns, selecting rows that have a given column value, etc., with Example R Scripts.