new programmer here. Have spent an hour trying to figure this error out. I'm sure I'm missing something, I just don't know what.
update: found a workaround, new question at the bottom
I loaded a BLS spreadsheet, and saw that all of the columns are characters, when I need them to be numeric. I applied as.numeric, but when I check the class type, it remains character.
> state2<-read_excel("Spreadsheets/state_M2022_dl.xlsx")
> str(state2)
tibble [37,569 × 32] (S3: tbl_df/tbl/data.frame)
$ AREA : chr [1:37569] "01" "01" "01" "01" ...
$ AREA_TITLE : chr [1:37569] "Alabama" "Alabama" "Alabama" "Alabama" ...
$ AREA_TYPE : chr [1:37569] "2" "2" "2" "2" ...
$ PRIM_STATE : chr [1:37569] "AL" "AL" "AL" "AL" ...
When I try as.numeric and check the class:
> as.numeric(state2$OWN_CODE)
[1] 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235
[24] 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235 1235
> class(state2$OWN_CODE)
[1] "character"
I see a few posts with a similar error, but they involve dataframes created in R, not imported spreadsheets.
update:
The following workaround seems to work -- is there a shorter/more efficient way of doing this than writing this out for every variable?
state2$H_PCT25<-as.numeric(state2$H_PCT25)
Warning message: NAs introduced by coercion
class(state2$H_PCT25) [1] "numeric"