Data Frames in R Language are generic data objects of R that are used to store tabular data.
Data frames can also be interpreted as matrices where each column of a matrix can be of different data
types.
Create Dataframe in R Programming Language
To create an R data frame use [Link]() function and then pass each of the vectors
[Link] <- [Link](
friend_id = c(1:5),
friend_name = c("Sachin", "Sourav",
"Dravid", "Sehwag",
"Dhoni"),
stringsAsFactors = FALSE
)
print([Link])
>Get the Structure of the R Data Frame
One can get the structure of the R data frame using str() function in R.
print(str([Link]))
>Summary of Data in the R data frame
In the R data frame, the statistical summary and nature of the data can be obtained by applying
summary() function.
print(summary([Link]))
Extract Data from Data Frame in R
Extracting data from an R data frame means that to access its rows or columns. One can extract a
specific column from an R data frame using its column name.
result <- [Link]([Link]$friend_name)
print(result)
Expand Data Frame in R Language
A data frame in R can be expanded by adding new columns and rows to the already
[Link]$location <- c("Kolkata", "Delhi",
"Bangalore", "Hyderabad",
"Chennai")
resultant <- [Link]
# print the modified data frame
print(resultant)
Access Items in R Data Frame
We can select and access any element from data frame by using single $ ,brackets [ ] or double brackets
[[]] to access columns from a data frame.
# Access Items using []
[Link][1]
# Access Items using [[]]
[Link][['friend_name']]
# Access Items using $
[Link]$friend_id
Amount of Rows and Columns
dim([Link])
Add Rows and Columns in R Data Frame
Add Rows in R Data Frame
To add rows in a Data Frame, you can use a built-in function rbind().
New_friends<- c(10, "ajay", 39, "kolar")
[Link]<- rbind([Link], New_friends)
[Link]
Add Columns in R Data Frame
To add columns in a Data Frame, you can use a built-in function cbind().
# Adding a new column for 'phno' to the dataframe
phno <- c(5333, 1033, 8333,4444,2323,3333)
[Link] <- cbind([Link], phno)
Remove Rows and Columns
A data frame in R removes columns and rows from the already existing R data frame.
[Link] <- subset([Link], friend_id != 3)
[Link]
[Link] <- subset([Link], !(friend_id %in% c(3,2,4)))
Remove Column in R DataFrame
library(dplyr)
# Create a data frame
data <- [Link](
friend_id = c(1, 2, 3, 4, 5),
friend_name = c("Sachin", "Sourav", "Dravid", "Sehwag", "Dhoni"),
location = c("Kolkata", "Delhi", "Bangalore", "Hyderabad", "Chennai")
)
data
# Remove the 'location' column
data <- select(data, -location)
data
Combine R Data Frame Vertically
If you want to combine 2 data frames vertically, you can use rbind() function.
# Creating two sample dataframes
df1 <- [Link](
Name = c("Alice", "Bob"),
Age = c(25, 30),
Score = c(80, 75)
)
df2 <- [Link](
Name = c("Charlie", "David"),
Age = c(28, 35),
Score = c(90, 85)
)
# Print the existing dataframes
cat("Dataframe 1:\n")
print(df1)
cat("\nDataframe 2:\n")
print(df2)
# Combining the dataframes using rbind()
combined_df <- rbind(df1, df2)
# Print the combined dataframe
cat("\nCombined Dataframe:\n")
print(combined_df)
Combine R Data Frame Horizontally:
If you want to combine 2 data frames horizontally, you can use cbind() function.
# Creating two sample dataframes
df1 <- [Link](
Name = c("Alice", "Bob"),
Age = c(25, 30),
Score = c(80, 75)
)
df2 <- [Link](
Height = c(160, 175),
Weight = c(55, 70)
)
# Print the existing dataframes
cat("Dataframe 1:\n")
print(df1)
cat("\nDataframe 2:\n")
print(df2)
# Combining the dataframes using cbind()
combined_df <- cbind(df1, df2)
# Print the combined dataframe
cat("\nCombined Dataframe:\n")
print(combined_df)