Untitled Flashcards Set

  1. ggplot() - Initializes a ggplot object; typically followed by aes() and geom_*() layers.

    • Example: ggplot(data = df, aes(x = var1, y = var2)) + geom_point()

  2. aes() - Defines aesthetic mappings, such as x and y variables, color, shape, size, etc.

    • Example: aes(x = var1, y = var2, color = category)

  3. geom_boxplot() - Creates a boxplot to visualize distribution and outliers of numerical data.

    • Example: ggplot(df, aes(x = factor, y = value)) + geom_boxplot()

  4. geom_point() - Creates a scatterplot for visualizing relationships between two numerical variables.

    • Example: ggplot(df, aes(x = var1, y = var2)) + geom_point()

  5. geom_histogram() - Creates a histogram to show the distribution of a single numerical variable.

    • Example: ggplot(df, aes(x = var1)) + geom_histogram(bins = 30, fill = "blue")

  6. geom_smooth() - Adds a smoothed conditional mean line (such as a LOESS or linear regression curve).

    • Example: ggplot(df, aes(x = var1, y = var2)) + geom_smooth(method = "lm")

  7. geom_line() - Creates a line chart, typically used for time series data.

    • Example: ggplot(df, aes(x = date, y = value)) + geom_line()

  8. labs() - Adds labels for title, axis labels, and legend.

    • Example: + labs(title = "Plot Title", x = "X-Axis", y = "Y-Axis", color = "Legend")

  9. theme_minimal() - Applies a clean, minimalistic theme to the plot.

    • Example: ggplot(df, aes(x, y)) + geom_point() + theme_minimal()

  10. theme() - Customizes various aspects of the plot, such as text size, legend position, and axis appearance.

    • Example: + theme(legend.position = "bottom", text = element_text(size = 12))

  11. scale_x_continuous() - Adjusts the x-axis scale, setting breaks, labels, or limits.

    • Example: + scale_x_continuous(limits = c(0, 100), breaks = seq(0, 100, 10))

  12. scale_color_manual() - Manually sets colors for different groups in a plot.

    • Example: + scale_color_manual(values = c("group1" = "red", "group2" = "blue"))

Data Inspection Functions (dplyr/tidyverse)

  1. glimpse() - Provides a compact summary of a dataframe’s structure.

    • Example: glimpse(df)

  2. nrow() - Returns the number of rows in a dataframe.

    • Example: nrow(df)

  3. ncol() - Returns the number of columns in a dataframe.

    • Example: ncol(df)

  4. dim() - Returns both the number of rows and columns in a dataframe.

    • Example: dim(df)

Data Manipulation Functions (dplyr/tidyr)

  1. slice_head() - Returns the first n rows of a dataframe.

    • Example: slice_head(df, n = 10)

  2. filter() - Filters rows based on a condition.

    • Example: filter(df, var1 > 50 & category == "A")

  3. arrange() - Sorts rows by one or more columns.

    • Example: arrange(df, desc(var1))

  4. relocate() - Moves selected columns to a new position in the dataframe.

    • Example: relocate(df, new_col, .before = first_col)

  5. if_else() - A vectorized conditional function returning values based on a condition.

    • Example: df <- df %>% mutate(new_var = if_else(var1 > 50, "High", "Low"))

  6. case_when() - A more flexible conditional function for multiple conditions.

    • Example: df <- df %>% mutate(category = case_when(var1 > 75 ~ "High", var1 > 50 ~ "Medium", TRUE ~ "Low"))

  7. count() - Counts occurrences of values in a column.

    • Example: count(df, category)

  8. group_by() - Groups data for grouped operations.

    • Example: df %>% group_by(category) %>% summarize(avg = mean(var1))

  9. ungroup() - Removes grouping from a dataframe.

    • Example: df <- df %>% ungroup()

  10. read_csv() - Reads a CSV file into a dataframe.

    • Example: df <- read_csv("data.csv")

  11. separate() - Splits a column into multiple columns based on a delimiter.

    • Example: df <- df %>% separate(full_name, into = c("first", "last"), sep = " ")

  12. mutate() - Creates or modifies columns.

    • Example: df <- df %>% mutate(new_col = var1 * 2)

  13. summarize() - Aggregates data based on a function (e.g., mean, sum, count).

    • Example: df %>% summarize(mean_value = mean(var1, na.rm = TRUE))

  14. pivot_longer() - Converts multiple columns into two columns: one for column names and one for values.

    • Example: df <- df %>% pivot_longer(cols = starts_with("Q"), names_to = "Quarter", values_to = "Sales")

  15. pivot_wider() - Converts key-value pairs into multiple columns.

    • Example: df <- df %>% pivot_wider(names_from = category, values_from = value)

  16. left_join() - Merges two dataframes, keeping all rows from the left dataframe and matching rows from the right.

    • Example: df <- left_join(df1, df2, by = "id")

  17. inner_join() - Merges two dataframes, keeping only rows that appear in both.

    • Example: df <- inner_join(df1, df2, by = "id")

  18. right_join() - Merges two dataframes, keeping all rows from the right dataframe.

    • Example: df <- right_join(df1, df2, by = "id")

  19. full_join() - Merges two dataframes, keeping all rows from both.

    • Example: df <- full_join(df1, df2, by = "id")