Import your data

symbols <- c("AMZN", "META", "NFLX", "NVDA", "GOOGL")
prices <- tq_get(x    = symbols,
                 get  = "stock.prices", 
                 from = "2012-12-31")

Create Data frame functions

Example 1: count columns

code snippets

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

ncol_num
## [1] 14

Turn them into a function

count_ncol_numeric <- function(.data) {
    
    # body
    ncol_num <- .data %>%
    
        # Select a type of variables
        select(where(is.numeric)) %>%
        
        # Count columns
        ncol()

    # return the new variable
    return(ncol_num)
    
}

flights %>% count_ncol_numeric()
## [1] 14
flights %>% .[1:10, -1:-13] %>% count_ncol_numeric()
## [1] 4

Adding arguments for details of operation

count_ncol_type <- function(.data, type_data = "numeric") {
    
    # if statement for type of variables
    if(type_data == "numeric") {
        # body
        ncol_type <- .data %>%
        
            # Select a type of variables
            select(where(is.numeric)) %>%
            
            # Count columns
            ncol()    
    } else if (type_data == "character") {
          # body
        ncol_type <- .data %>%
        
            # Select a type of variables
            select(where(is.character)) %>%
            
            # Count columns
            ncol()
    }
    
    # return the new variable
    return(ncol_type)
    
}

flights %>% count_ncol_type()
## [1] 14
flights %>% count_ncol_type(type_data = "character")
## [1] 4
flights %>% .[1:10, 1:5] %>% count_ncol_type(type_data = "character")
## [1] 0

Example 2: count rows

code snippets

nrow_num <- flights %>%
    
    # filter rows that meet a condition
    filter(carrier == "DL") %>%
    
    # Count rows
    nrow()

nrow_num
## [1] 48110

Turn them into a function

count_num_flights_by_carrier <- function(.data, carrier_name) {
    
    # body
    nrow_num <- .data %>%
        
        # filter rows that meet a condition
        filter(carrier == carrier_name) %>%
        
        # Count rows
        nrow()
    
    # return the new variable
    return(nrow_num)
}

flights %>% .[1:10, "carrier"] %>% count_num_flights_by_carrier(carrier_name = "AA")
## [1] 2

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()

nrow_num <- prices %>%
    
    
    # filter rows that meet a condition
    filter(volume < 2000000) %>%
    
    # Count rows
    nrow()

nrow_num
## [1] 48

Turn them into a function

filter_low_volume <- function(threshold = 2000000) {
    
    filtered_data <- prices %>%
    
        # filter rows that meet a condition
        filter(volume < threshold) 
    
    return(filtered_data)
    
}

filter_low_volume()
## # A tibble: 48 × 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 NFLX   2016-11-25  118.  118.  117.  117. 1616300     117.
##  2 NFLX   2020-12-22  528.  530.  520.  527. 1906600     527.
##  3 NFLX   2020-12-24  515.  519.  512.  514. 1144000     514.
##  4 NFLX   2020-12-30  530.  533.  524.  525. 1876300     525.
##  5 NFLX   2021-07-02  536.  539.  529.  534. 1975500     534.
##  6 NFLX   2021-07-12  540.  541.  533.  537. 1780700     537.
##  7 NFLX   2021-07-29  520.  521.  514.  514. 1736000     514.
##  8 NFLX   2021-08-06  524   527.  519.  521. 1919800     521.
##  9 NFLX   2021-08-09  521.  523.  518.  520. 1367800     520.
## 10 NFLX   2021-08-10  520   521.  513.  516. 1960500     516.
## # ℹ 38 more rows