Import your data

babynames<- read_excel("../00_data/mydata2.xlsx")

# Top 15 male names
top15_male <- babynames %>%
  filter(sex == "M") %>%
  group_by(Name) %>%
  summarize(Proportion = sum(Proportion)) %>%
  arrange(desc(Proportion)) %>%
  slice_head(n = 15)

# Top 15 female names
top15_female <- babynames %>%
  filter(sex == "F") %>%
  group_by(Name) %>%
  summarize(Proportion = sum(Proportion)) %>%
  arrange(desc(Proportion)) %>%
  slice_head(n = 15)

Create Data frame functions

Example 1: count columns

code snippets

ncol_num <- babynames %>%
    
    # Select a type of variables
    select(where(is.numeric)) %>%
    
    # Count columns
    ncol()

ncol_num
## [1] 3

Turn them into a function

count_ncol_numeric <- function(.data) {

  ncol_num <- .data %>%

    # Select a type of variables
    select(where(is.numeric)) %>%

    # Count columns
    ncol()

  # Return the new variable
  return(ncol_num)
}

babynames %>% count_ncol_numeric()
## [1] 3
top15_male %>% count_ncol_numeric()
## [1] 1
top15_female %>% count_ncol_numeric()
## [1] 1

Adding arguments for details of operation

count_ncol_type <- function(.data, type_data = "numeric") {

  # If statement for type of variables
  if(type_data == "numeric") {

    ncol_type <- .data %>%

      # Select numeric variables
      select(where(is.numeric)) %>%

      # Count columns
      ncol()

  } else if (type_data == "character") {

    ncol_type <- .data %>%

      # Select character variables
      select(where(is.character)) %>%

      # Count columns
      ncol()

  }

  return(ncol_type)
}

babynames %>% count_ncol_type()
## [1] 3
babynames %>% count_ncol_type(type_data = "character")
## [1] 2
top15_male %>% count_ncol_type(type_data = "character")
## [1] 1
top15_female %>% count_ncol_type(type_data = "character")
## [1] 1

Example 2: count rows

code snippets

row_num_male <- babynames %>%

  # Filter rows that meet a condition
  filter(sex == "M") %>%

  # Count rows
  nrow()

row_num_male
## [1] 426150
row_num_female <- babynames %>%

  # Filter rows that meet a condition
  filter(sex == "F") %>%

  # Count rows
  nrow()

row_num_female
## [1] 622425

Turn them into a function

count_rows <- function(.data) {

  # Count rows
  row_num <- .data %>%
    nrow()

  # Return the new variable
  return(row_num)
}

babynames %>% count_rows()
## [1] 1048575
top15_male %>% count_rows()
## [1] 15
top15_female %>% count_rows()
## [1] 15

Example 3: count rows

Create your own.

code snippets

Use the filter() function to select rows that meet a condition. Refer to Chapter 5.2 Filter rows with filter()

# Count male names
row_num_male <- babynames %>%

  # Filter rows that meet a condition
  filter(sex == "M") %>%

  # Count rows
  nrow()

row_num_male
## [1] 426150

for females

row_num_female <- babynames %>%

  # Filter rows that meet a condition
  filter(sex == "F") %>%

  # Count rows
  nrow()

row_num_female
## [1] 622425

Turn them into a function

count_rows <- function(.data, sex_type) {

  # Filter rows that meet a condition
  row_num <- .data %>%
    filter(sex == sex_type) %>%

    # Count rows
    nrow()

  # Return the new variable
  return(row_num)
}

# Count male names
babynames %>% count_rows("M")
## [1] 426150
# Count female names
babynames %>% count_rows("F")
## [1] 622425