A Data Frame is a table or a 2D structure in R that can store data of different types (numeric, character, factor, etc.) in each column. It is the most commonly used data structure for storing datasets in R.
# Create a data frame students <- data.frame( Name = c("Alice", "Bob", "Charlie"), Age = c(22, 24, 23), Score = c(89.5, 92.3, 88.0) )
This creates a table with 3 columns: Name, Age, and Score.
print(students)
– View entire data framehead(students)
– View first 6 rowsstr(students)
– Structure of datasummary(students)
– Summary statisticsstudents$Name
– Access "Name" columnstudents[1, ]
– First rowstudents[ ,2]
– Second columnstudents[2, 3]
– 2nd row, 3rd column# Add a new column students$Passed <- c(TRUE, TRUE, FALSE) # Add a new row new_row <- data.frame(Name="David", Age=21, Score=85.6, Passed=TRUE) students <- rbind(students, new_row)
# Change a value students$Score[2] <- 95.0
# Remove a column students$Passed <- NULL # Remove a row students <- students[-1, ]
Use conditions to filter rows:
# Students with Score > 90 high_scorers <- students[students$Score > 90, ]
# Sort by Score students_sorted <- students[order(students$Score), ]
$
, index positions, or logical conditions.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!