Week 4: Organizing Data with Lists & Data Frames
Learn about R's flexible lists and the essential data frame structure.
Explore Chapter 4Viewing and Inspecting Data Frames.
When working with data frames, especially larger ones, it's essential to have tools to inspect their structure and content without printing the entire dataset.
- `head(df, n=6)`: Shows the first `n` rows (default is 6).
- `tail(df, n=6)`: Shows the last `n` rows (default is 6).
- `str(df)`: Displays the structure compactly, showing the total observations (rows), variables (columns), the data type of each column, and the first few values. Highly recommended!
- `summary(df)`: Provides a statistical summary for each column (min, max, mean, median, quartiles for numeric; counts for factors/characters).
- `dim(df)`: Returns the dimensions (number of rows and columns) as a vector `c(rows, cols)`.
- `nrow(df)`: Returns the number of rows.
- `ncol(df)`: Returns the number of columns.
- `names(df)` or `colnames(df)`: Returns the column names.
- `rownames(df)`: Returns the row names (often just sequence numbers by default).
# Assuming 'my_dataframe' from the previous section
head(my_dataframe)
str(my_dataframe)
summary(my_dataframe)
dim(my_dataframe)
names(my_dataframe)