I have these two functions intended to load a data set (https://pages.scinet.utoronto.ca/~ejspence/1710014201-eng.csv) and calculate the frequency of each first digit in the years columns
# Function that loads the appropriate data
load.data <-function(csvfile) {
raw.data <-read.csv(csvfile, na.strings = "..")
raw.data <-data.frame(raw.data)
raw.data[raw.data==0] <-NA
my.data <-data.frame(raw.data[complete.cases(raw.data),])
return(my.data)
}
# Function takes two arguments: 1)Dataframe with population data and 2) year (2017-2021)
# Calculates the frequency of each 1st digit and returns digits and frequencies in dataframe
build.digit.freqs <- function(dataframe, year) {
ext.first.digit <- function(x) return(substr(x,1,1))
first.digits <- sapply(dataframe$year, ext.first.digit)
my.freqs <- as.data.frame(table(first.digits))
colnames(my.freqs) <- c("digits", "counts")
return(my.freqs)
}
When I run the second function step by step in the command line, everything seems to work perfectly, however, when I try to run the function as a whole with the arguments build.digit.freqs(my.data, 2017) I get the error message:
Error in table(first.digits) : nothing to tabulate
Not sure what I'm missing
I have tried renaming the column names (2017, "2017", X2017) and it does not seem to fix the problem. When I run each line of the function from the command line, everything appears to be working properly.