CodeToLive

Data Frames in R

Data frames are tabular data structures in R, similar to tables in a database or Excel sheets. They are widely used for storing and manipulating structured data.

Creating a Data Frame

Data frames can be created using the data.frame function.


df <- data.frame(
    name = c("Alice", "Bob", "Charlie"),
    age = c(25, 30, 35),
    is_student = c(TRUE, FALSE, TRUE)
)

print(df)
      

Accessing Data

You can access columns and rows in a data frame using the $ operator and indexing.


# Accessing a Column
print(df$name)

# Accessing a Row
print(df[1, ])
      

Adding and Removing Columns

You can add or remove columns from a data frame using the $ operator and the NULL assignment.


# Adding a Column
df$salary <- c(50000, 60000, 70000)

# Removing a Column
df$is_student <- NULL

print(df)
      

Filtering Data

You can filter rows in a data frame using logical conditions.


# Filtering Rows
filtered_df <- df[df$age > 30, ]
print(filtered_df)
      

Merging Data Frames

You can merge two data frames using the merge function.


df1 <- data.frame(id = c(1, 2, 3), name = c("Alice", "Bob", "Charlie"))
df2 <- data.frame(id = c(1, 2, 4), salary = c(50000, 60000, 70000))

# Merging Data Frames
merged_df <- merge(df1, df2, by = "id")
print(merged_df)
      

Summarizing Data

You can summarize data using functions like summary and aggregate.


# Summary Statistics
print(summary(df))

# Aggregating Data
print(aggregate(age ~ is_student, data = df, FUN = mean))
      

Reading and Writing Data

You can read data from and write data to CSV files using read.csv and write.csv.


# Reading from a CSV File
df <- read.csv("data.csv")

# Writing to a CSV File
write.csv(df, "output.csv", row.names = FALSE)
      
Back to Tutorial