R Programming language - R Data Frame

In R programming language, a data frame is a two-dimensional data structure that stores data in rows and columns, similar to a spreadsheet or a SQL table. Each column can have a different data type, such as numeric, character, or factor, and each row represents a single observation or record.

Here is an example of a data frame in R:

re‮t ref‬o:theitroad.com
df <- data.frame(name = c("Alice", "Bob", "Charlie"),
                 age = c(25, 30, 35),
                 gender = c("female", "male", "male"))

In this example, df is a data frame with three columns: name, age, and gender, and three rows representing three individuals.

You can access individual columns or rows of a data frame using the $ operator or square bracket notation, like this:

df <- data.frame(name = c("Alice", "Bob", "Charlie"),
                 age = c(25, 30, 35),
                 gender = c("female", "male", "male"))

print(df$name) # Output: "Alice" "Bob" "Charlie"
print(df[2, ]) # Output: "Bob" 30 "male"

In this example, df$name refers to the name column of the data frame, and df[2, ] refers to the second row of the data frame.

You can modify individual elements of a data frame using square bracket notation and the assignment operator, like this:

df <- data.frame(name = c("Alice", "Bob", "Charlie"),
                 age = c(25, 30, 35),
                 gender = c("female", "male", "male"))

df[2, 2] <- 31
print(df[2, ]) # Output: "Bob" 31 "male"

In this example, df[2, 2] refers to the age of the second individual in the data frame, and the value is changed from 30 to 31.

Understanding how to work with data frames in R is essential for data analysis and manipulation tasks, especially when dealing with tabular data or working with datasets that require joining, merging, or filtering operations.