HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Data Frames are data displayed in a format as a table
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
Data_Frame <- data.frame (
Training = c("Strength", "Stamina",
"Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30,
45)
)
# Print the data frame
Data_Frame
Use the summary()
function to summarize the data 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
summary(Data_Frame)
You will learn more about the summary()
function in the statistical part of the R tutorial.
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:
Data_Frame <- data.frame (
Training = c("Strength", "Stamina",
"Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30,
45)
)
# 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:
Data_Frame <- data.frame (
Training = c("Strength", "Stamina",
"Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30,
45)
)
# Add a new column
New_col_DF <- cbind(Data_Frame, Steps =
c(1000, 6000, 2000))
# Print the new column
New_col_DF
Use the c()
function to remove rows and columns in a Data Frame:
Data_Frame <- data.frame (
Training = c("Strength", "Stamina",
"Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30,
45)
)
# Remove the first row and column
Data_Frame_New <-
Data_Frame[-c(1), -c(1)]
# Print the new data frame
Data_Frame_New
Use the dim()
function to find the amount of rows and columns in a Data Frame:
Data_Frame <- data.frame (
Training = c("Strength", "Stamina",
"Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30,
45)
)
dim(Data_Frame)
You can also use the ncol()
function to find the number of columns and nrow()
to find the number of rows:
Data_Frame <- data.frame (
Training = c("Strength", "Stamina",
"Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30,
45)
)
ncol(Data_Frame)
nrow(Data_Frame)
Use the length()
function to find the number of columns in a Data Frame (similar to ncol()
):
Data_Frame <- data.frame (
Training = c("Strength", "Stamina",
"Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30,
45)
)
length(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)
New_Data_Frame
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)
New_Data_Frame1