0

so this is my code so far. My extractDate works and gives me "##" formats. However, I'm having a big of trouble with getYears function- my intention is given in the code comments below. Basically, I want to attach either 19 or 20 or 200 to numbers based on the value it has. I keep getting this error:

Error in if (as.numeric(spotify$rel_date[i]) > 22) { : 
  missing value where TRUE/FALSE needed

Can anyone please help me out or provide an alternative solution to my problem? Thank you!!!

The spotify release dates are either formatted as dd-mmm-yy / mmm-yy / yyyy
## To standardize the years, extract the last two numbers and add 19 or 20 to prefix

##' extractDate function
##' @string is the Date in string format to extract the year from
##' @n is the number of characters to extract from the string
##' substr(x, start, stop) from RDocumentation

extractDate = function(string, n){
  extract =substr(string, nchar(string)-n+1, nchar(string))
}

##' getYears function
##' @string is the Date in string format to add onto to make a proper year
##' For any numbers "##" greater than 22, we will attach "19" in the prefix (i.e.: "1967")
##' For any numbers "##" less or equal to 22, we will attach "20" in the prefix (i.e: "2006")

getYears = function(string){
  if (as.numeric(string)>22){
    string=paste0("19",string)
  }
  else {
    string=paste0("20",string)
  }
}

## spoti$rel_date=extractDate(spoti$rel_date,2)

spotify$rel_date=extractDate(spotify$rel_date,2)
spotify$rel_date


for (i in spotify$rel_date){
  if (as.numeric(spotify$rel_date[i])>22){
    spotify$rel_date[i]=paste0("19",spotify$rel_date[i])
  }
  else if (as.numeric(spotify$rel_date[i]<10)){
    spotify$rel_date[i]=paste0("200",spotify$rel_date[i])
  }
  else {
    spotify$rel_date[i]=paste0("20",spotify$rel_date[i])
  }
}
Melody
  • 11
  • 3
  • Run through your code line-by-line, then verify what seems likely: one of your `if` blocks is returning an `NA`. Fix the code that generates the issue. (It seems likely that you are getting warnings on the console as well.) It's difficult to give you a real resolution without sample data, but please check the dupe link and then trace your code for each of your `if` blocks. – r2evans Aug 15 '22 at 13:08
  • Found something: `for (i in spotify$rel_date)` is iterating over each _value_, but you are using `i` as if it is an _index_ on the values. Try `for (i in seq_along(spotify$rel_date))` (and keep the rest of your code as-is ... but still check for `NA` values). – r2evans Aug 15 '22 at 13:29
  • But this can likely be improved a *lot* with: `spotify$rel_date <- sprintf("%02i", as.numeric(spotify$rel_date)); spotify$rel_date <- paste0(ifelse(as.numeric(spotify$rel_date) > 22, "19", "20"), spotify$rel_date)`. (*No loops required.*) – r2evans Aug 15 '22 at 13:33

0 Answers0