Data frame

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:

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

We can use single brackets [ ], double brackets [[ ]] or $ to access columns from a data frame:

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

Data_Frame[1]
Data_Frame[["Training"]]
Data_Frame$Training

Use the rbind() function to add new rows in a Data Frame:

# Add a new row
New_row_DF <- rbind(Data_Frame, c("Strength", 110, 110))
# Print the new row
New_row_DF

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

# Add a new column
New_col_DF <- cbind(Data_Frame, Steps = c(1000, 6000, 2000))
# Print the new column
New_col_DF

Use the dim() function to find the amount of rows and columns in a Data Frame.
You can also use the ncol() function to find the number of columns and nrow() to find the number of rows:

dim(Data_Frame)
ncol(Data_Frame)
nrow(Data_Frame)

Use the rbind() function to combine two or more data frames in R vertically:

Data_Frame1 <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

Data_Frame2 <- data.frame (
  Training = c("Stamina", "Stamina", "Strength"),
  Pulse = c(140, 150, 160),
  Duration = c(30, 30, 20)
)

New_Data_Frame <- rbind(Data_Frame1, Data_Frame2)

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

Data_Frame3 <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

Data_Frame4 <- data.frame (
  Steps = c(3000, 6000, 2000),
  Calories = c(300, 400, 300)
)

New_Data_Frame1 <- cbind(Data_Frame3, Data_Frame4)

Extract rows

#extract row 2
df[2, ]

#extract rows 2, 4, and 5
df[c(2, 4, 5), ]

#extract rows in range of 1 to 3
df[1:3, ]

#extract rows where value in column1 is greater than 10
df[df$column1 > 10, ]

#extract rows where column1 > 10 and column2 > 5
df[df$column1 > 10 & df$column2 > 5, ]

#extract rows where column1 > 10 or column2 > 5
df[df$column1 > 10 | df$column2 > 5, ]
# 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))

# fetch 2,3 rows and 1,2 columns
stats[2:3,c(1,2)]

# fetch 1:3 rows of 1st column
cat("players - ")
stats[1:3,1]