Import your data

symbols <- c("WMT", "AAPL", "NFLX", "GOOGL")

prices <- tq_get(x   = symbols,
                get  = "stock.prices",
                from = "2012-12-31")
prices
## # A tibble: 13,980 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 WMT    2012-12-31  22.5  22.8  22.5  22.7 21037500     17.4
##  2 WMT    2013-01-02  23.0  23.1  22.8  23.1 31172400     17.7
##  3 WMT    2013-01-03  23.1  23.1  22.8  22.9 26730300     17.6
##  4 WMT    2013-01-04  22.9  23.1  22.8  23.0 19314000     17.7
##  5 WMT    2013-01-07  22.9  23.0  22.7  22.8 18604200     17.5
##  6 WMT    2013-01-08  22.8  23.0  22.7  22.9 17600700     17.5
##  7 WMT    2013-01-09  22.9  22.9  22.7  22.9 15165600     17.5
##  8 WMT    2013-01-10  22.9  23.0  22.6  22.8 34361400     17.5
##  9 WMT    2013-01-11  22.9  22.9  22.7  22.9 18673500     17.6
## 10 WMT    2013-01-14  22.8  22.9  22.7  22.8 16471200     17.5
## # ℹ 13,970 more rows

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 == "UA") %>%
    
    # Count rows
    nrow()

nrow_num
## [1] 58665

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_numb <- prices %>% 
    
    # filter rows that meet a condition
    filter(open >= 22.5) %>% 
    
    # count rows
    nrow()

nrow_numb
## [1] 11927

Turn them into a function

count_num_rows_by_open_value <- function(.data, open) {
    
    # body
     nrow_numb <- .data %>%
        
        # filter rows that meet a condition
        filter(open == open) %>%
        
        # Count rows
        nrow()
   
    
    # return the new variable
    return(nrow_numb)
}

prices %>% count_num_rows_by_open_value(open >= 24.0)
## [1] 13980