0

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.

  • Welcome to SO, Micaela! (1) R generally works better with vectorized operations, you can replace your `sapply(...)` with `substr(dataframe$year, 1, 1)` for the same effect (but faster). (2) You have `year` as an argument to the function but never use it. What's the intent there? (3) Are we supposed to do something with `load.data`? It seems superfluous to your issue. – r2evans Nov 01 '22 at 17:44
  • Forget that ... your issue is that `dataframe$year` is `NULL`. You need `dataframe[[year]]`, since `$year` is looking for a column named the literal `"year"`, not the value you're trying to pass in. – r2evans Nov 01 '22 at 17:46
  • The dupe-link is titled for `\`[\` versus \`[[\``, but it also touches on the use of `$` as well. Please @ping me if my previous comment and that link do not get you working. – r2evans Nov 01 '22 at 17:47

0 Answers0