bigglesworth
bigglesworth

Reputation: 3

Function using default argument despite value being supplied in call

I'm calling a function with x = 14 because the folder only has files 0-13 instead of 0-23 like the others, but it gives me an error saying 14.csv isn't in the directory, but it should stop at 13.csv. The code worked when I just put a 14 in place of the x, so why isn't it working when I call the function with x = 14? It seems like it's using the default argument. Relevant code below.

library(tidyverse)
library(lubridate)

files <- c("0.csv", "1.csv", "2.csv", "3.csv", "4.csv", "5.csv", "6.csv", "7.csv", "8.csv", "9.csv", "10.csv", "11.csv", "12.csv", "13.csv", "14.csv", "15.csv", "16.csv", "17.csv", "18.csv", "19.csv", "20.csv", "21.csv", "22.csv", "23.csv")

dayFiles <- function(x = length(files)) {
  output <- vector("list", x)
  for (i in seq_along(files)) {
    output[[i]] <- read_csv(files[[i]],
                            col_types = cols(
                              `DBM Matching Targeted Segments` = col_character(),
                              `DBM Matching Targeted Keywords` = col_character()
                            )) %>%
      filter(`DBM Insertion Order ID` == 9999999) %>%
      select(`Event Time`, `DBM Insertion Order ID`, `User ID`)
    
  }
  for (i in 2:length(output)) {
    output[[i]] <- full_join(output[[i-1]], output[[i]])
  }
  return(output[[length(output)]])
}

A24 <- dayFiles(x = 14)

Upvotes: 0

Views: 39

Answers (2)

Ric
Ric

Reputation: 5721

because in this line:

for (i in seq_along(files)) {

you're looping the entire vector instead of 1:14

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389175

Your loop is iterating over files which is of length 24. Instead it should create a sequence between 1:x. Try :

dayFiles <- function(x = length(files)) {

  output <- vector("list", x)
  for (i in seq_len(x)) {
    output[[i]] <- read_csv(files[[i]],
                            col_types = cols(
                              `DBM Matching Targeted Segments` = col_character(),
                              `DBM Matching Targeted Keywords` = col_character()
                            )) %>%
      filter(`DBM Insertion Order ID` == 9999999) %>%
      select(`Event Time`, `DBM Insertion Order ID`, `User ID`)
    
  }
  for (i in 2:length(output)) {
    output[[i]] <- full_join(output[[i-1]], output[[i]])
  }
  return(output[[length(output)]])
}

A24 <- dayFiles(x = 14)

To further simplify, instead of joining the dataframes in a loop you might try using reduce(output, full_join).

Upvotes: 0

Related Questions