A data frame is R’s table: each column is a vector with one type (text, number, logical), and all columns have the same length. It is the most used structure for real data, like a spreadsheet or a pandas DataFrame.
What You Will Learn
- Create and summarise a data frame
- Select columns and filter rows
- Add rows and columns correctly
- Remove rows and columns
- Combine data frames with rbind() and cbind()
- Read and write CSV files
Prerequisites
Create a Data Frame
df <- data.frame(
Name = c("Karan", "Keisha", "Atharv"),
Age = c(30, 23, 27),
Marks = c(89, 97, 70)
)
print(df)
str(df)
Name Age Marks 1 Karan 30 89 2 Keisha 23 97 3 Atharv 27 70 'data.frame': 3 obs. of 3 variables: $ Name : chr "Karan" "Keisha" "Atharv" $ Age : num 30 23 27 $ Marks: num 89 97 70
Explanation: str() shows the type of each column. Since R 4.0, text columns stay as chr; older R converted them to factors, which is why old outputs show “Levels”.
Summarise
summary(df[, c("Age", "Marks")])
dim(df); nrow(df); ncol(df)
Age Marks Min. :23.00 Min. :70.00 1st Qu.:25.00 1st Qu.:79.50 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 [1] 3 3 [1] 3 [1] 3
Select and Filter
df$Name
df[["Marks"]]
df[df$Marks > 80, ]
df[df$Age < 28, c("Name", "Marks")]
[1] "Karan" "Keisha" "Atharv"
[1] 89 97 70
Name Age Marks
1 Karan 30 89
2 Keisha 23 97
Name Marks
2 Keisha 97
3 Atharv 70
Explanation: df[rows, columns]: a logical condition selects rows; a vector of names selects columns. Leaving one side empty means “all”.
Add Rows and Columns
new_row <- data.frame(Name = "Priya", Age = 21, Marks = 99)
df1 <- rbind(df, new_row)
df1$Roll_No <- c(10, 11, 12, 13)
str(df1)
'data.frame': 4 obs. of 4 variables: $ Name : chr "Karan" "Keisha" "Atharv" "Priya" $ Age : num 30 23 27 21 $ Marks : num 89 97 70 99 $ Roll_No: num 10 11 12 13
c("Priya", 88, 99). Because a vector holds one type, that turned Age and Marks into text (chr), so maths on them stopped working. Add rows as a one-row data frame (or list) to keep the types.Remove Rows and Columns
df[-1, -1] # drop first row and first column
df$Age <- NULL # drop a column by name
Age Marks 2 23 97 3 27 70
Combine Data Frames
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))
rbind(df1, df2) # stack vertically (same columns)
merge(df1, data.frame(Name = "Karan", City = "Ludhiana"), by = "Name") # join by key
Name Age Marks 1 Karan 30 89 2 Keisha 23 97 3 Atharv 27 70 4 Akshay 31 69 5 Robin 24 91 6 Manish 28 77 Name Age Marks City 1 Karan 30 89 Ludhiana
Explanation: rbind stacks data frames with the same columns. cbind places them side by side (columns must have the same number of rows). merge joins on a key column, like a SQL JOIN.
Read and Write CSV
write.csv(df1, "students.csv", row.names = FALSE)
students <- read.csv("students.csv")
head(students)
Series
R series: Install R · Introduction to R · R in Jupyter · Practical Session · Operators · Decision Making · Loops · Functions · Strings · cat() and paste() · Vectors · Lists · Matrices · Data Frames
Interview Questions
Q: Data frame vs matrix?
A matrix holds one type; a data frame can have a different type in each column.
Q: Why did adding c(“Priya”, 88, 99) break numeric columns?
The vector was coerced to character, which changed the column types.
Practice Exercises
- Create a data frame of 5 products with price and quantity, add a total column and filter totals above 1000.
- Read a CSV, compute the mean of a column and save the filtered rows to a new file.
Summary
Data frames are R’s tables. Create them with data.frame(), inspect with str()/summary(), select with $ and [rows, cols], add rows as data frames to keep types, and combine with rbind, cbind and merge.
Machine Learning Tutorials, Courses and Certifications