I have:
library(tidyverse)
library(lubridate)
data <- tibble(date = ymd("20220224"),
number = 1234,
decimal = 12.34,
char = "20220224")
)
I want to convert the date column to character, and keep the exact format, ie 2022-02-24
performing as.character(ymd("20220224")) works perfectly, and I get "2022-02-24"
but, when I apply this to the column, data[,"date"] = as.character(data[,"date"]), I get the char of the date counting days since Jan 1, 1970.
How can I get it so that I get the characters as desired? I want to see "2022-02-24", not "19047"
thanks
>Solution :
tibble will return a tibble after you have subsetted it using [. Therefore you need to use [[.
library(tidyverse)
library(lubridate)
data[,"date"] = as.character(data[["date"]])
# A tibble: 1 x 4
date number decimal char
<chr> <dbl> <dbl> <chr>
1 2022-02-24 1234 12.3 20220224
