R Tutorials by : Er Karan Arora : Founder & CEO - ITRONIX SOLUTIONS

Data Frames

  • Data Frames are data displayed in a format as a table.
  • Data Frames can have different types of data inside it. While the first column can be character, the second and third can be numeric or logical. However, each column should have the same type of data.
  • Use the data.frame() function to create a data frame:

Create a data frame

In [1]:
df <- data.frame (
  Name = c("Karan", "Keisha", "Atharv"),
  Age = c(30, 23, 27),
  Marks = c(89, 97, 70)
)
In [2]:
df
A data.frame: 3 × 3
NameAgeMarks
<fct><dbl><dbl>
Karan 3089
Keisha2397
Atharv2770

Summarize the Data

  • Use the summary() function to summarize the data from a Data Frame:
In [3]:
summary(df)
     Name        Age            Marks      
 Atharv:1   Min.   :23.00   Min.   :70.00  
 Karan :1   1st Qu.:25.00   1st Qu.:79.50  
 Keisha:1   Median :27.00   Median :89.00  
            Mean   :26.67   Mean   :85.33  
            3rd Qu.:28.50   3rd Qu.:93.00  
            Max.   :30.00   Max.   :97.00  

Access Items

  • We can use single brackets [ ], double brackets [[ ]] or $ to access columns from a data frame:
In [4]:
df[1]
A data.frame: 3 × 1
Name
<fct>
Karan
Keisha
Atharv
In [5]:
df[["Name"]]
  1. Karan
  2. Keisha
  3. Atharv
Levels:
  1. 'Atharv'
  2. 'Karan'
  3. 'Keisha'
In [6]:
df$Name
  1. Karan
  2. Keisha
  3. Atharv
Levels:
  1. 'Atharv'
  2. 'Karan'
  3. 'Keisha'

Add Rows

  • Use the rbind() function to add new rows in a Data Frame:
In [7]:
df$Name <- as.character(df$Name)
In [8]:
new_row <- c("Priya", 88, 99)
df1 <- rbind(df, new_row)
df1
A data.frame: 4 × 3
NameAgeMarks
<chr><chr><chr>
Karan 3089
Keisha2397
Atharv2770
Priya 8899

Add Columns

  • Use the cbind() function to add new columns in a Data Frame:

Add a new column

In [9]:
df2 <- cbind(df, Roll_No = c(10, 11, 12))
df2
A data.frame: 3 × 4
NameAgeMarksRoll_No
<chr><dbl><dbl><dbl>
Karan 308910
Keisha239711
Atharv277012

Remove Rows and Columns

  • Use the c() function to remove rows and columns in a Data Frame:

Remove the first row and column

In [10]:
df3 <- df[-c(1), -c(1)]
df3
A data.frame: 2 × 2
AgeMarks
<dbl><dbl>
22397
32770

Amount of Rows and Columns

  • Use the dim() function to find the amount of rows and columns in a Data Frame:
In [11]:
dim(df)
  1. 3
  2. 3
In [12]:
ncol(df)
3
In [13]:
nrow(df)
3

Data Frame Length

  • Use the length() function to find the number of columns in a Data Frame (similar to ncol()):
In [14]:
length(df)
3

Combining Data Frames

  • Use the rbind() function to combine two or more data frames in R vertically:
In [15]:
df1 <- data.frame (
  Name = c("Karan", "Keisha", "Atharv"),
  Age = c(30, 23, 27),
  Marks = c(89, 97, 70)
)
df2 <- data.frame (
  Name = c("Akshay", "Robin", "Manish"),
  Age = c(31, 24, 28),
  Marks = c(69, 91, 77)
)

df <- rbind(df1, df2)
df
A data.frame: 6 × 3
NameAgeMarks
<fct><dbl><dbl>
Karan 3089
Keisha2397
Atharv2770
Akshay3169
Robin 2491
Manish2877

And use the cbind() function to combine two or more data frames in R horizontally:

In [16]:
df <- cbind(df1, df2)
df
A data.frame: 3 × 6
NameAgeMarksNameAgeMarks
<fct><dbl><dbl><fct><dbl><dbl>
Karan 3089Akshay3169
Keisha2397Robin 2491
Atharv2770Manish2877