0

I have a column in my dataset called 'NAME'. Its in a character format. To make it use-able in R, do I have to convert it to a categorical variable? I'm thinking it will be factored into levels? Is that right?

Also I have a column called 'Date'. Also in a character format; in the order month and year e.g Jan 21. How do I convert this appropriately? I can't use as.Date(). I have tried the lubridate() function too but isn't working.i believe it has to do with my syntax.

Sorry I can't attach photos as it keeps displaying server error from my end.

Juliet
  • 1
  • 1
  • See [How to make a great R reproducible example]( https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – TarJae Oct 13 '22 at 17:20

1 Answers1

0
  1. Most R functions that perform regressions (lm, etc.) are smart enough to treat character type variables as categorical variables, but if you prefer, you can convert it yourself with as.factor().

  2. You can convert a date in ISO format with as.Date. The lubridate package has functions like mdy that can convert other formats.

halloween <- as.Date("2022-10-31")
print(halloween)
#> [1] "2022-10-31"
class(halloween)
#> [1] "Date"

halloween <- lubridate::mdy("10/31/2022")
print(halloween)
#> [1] "2022-10-31"
class(halloween)
#> [1] "Date"

Created on 2022-10-13 with reprex v2.0.2

Arthur
  • 1,248
  • 8
  • 14