0

Part of my assignment involves taking the previous table I modified and adding a new variable that extracts the first 4 numbers of a previous variable for this new variable. For some reason, even after using mutate or transmute to add the variable without affecting the variables in the previous table, I am still getting an empty table. I am not sure where exactly the problem is.

This is my code so far:

salary4 = transmute(salary3, HireYear = str_sub(str(Hire), 1, 4))
salary4

The error I keep getting for this problem is this: "HireYear` must be size 13001 or 1, not 0"

r2evans
  • 141,215
  • 6
  • 77
  • 149
SHANDH
  • 1
  • 2
  • 2
    Why you use `str` function. – Mohamed Desouky Jun 26 '22 at 22:32
  • 3
    Welcome to SO, SHANDH! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jun 26 '22 at 22:43
  • 1
    FYI, when the Stack interface suggests "code fences" for putting code into blocks, they mean the backtick `\``, not the single-quote `'`. See https://stackoverflow.com/editing-help and https://meta.stackexchange.com/a/22189. – r2evans Jun 26 '22 at 22:44
  • If you are trying to convert a number to a string, the appropriate function is `as.character()` not `str()`. However, there is no need to convert: `str_sub()` will work on numbers and do the conversion to string automatically. – neilfws Jun 26 '22 at 23:44

0 Answers0