df<-separate(df$ALISVERIS_TARIHI, c("key","value")," ", extra=merge)
Error in UseMethod("separate_") : no applicable method for 'separate_' applied to an object of class "character"
"20190901" how can I separate this into 3 columns like 2019 09 01?
CodePudding user response:
If you want to separate the 1st column based on number of characters you can use extract
as -
df <- tidyr::extract(df, ALISVERIS_TARIHI, c('year', 'month', 'day'), '(.{4})(..)(..)')
df
# year month day a
#1 2019 09 01 a
#2 2019 09 08 b
The same pattern can be used with strcapture
in base R -
data <- strcapture('(.{4})(..)(..)', df$ALISVERIS_TARIHI,
proto = list(year = integer(), month = integer(), day = integer()))
data
It is easier to help if you provide data in a reproducible format
df <- data.frame(ALISVERIS_TARIHI = c('20190901', '20190908'), a = c('a', 'b'))
CodePudding user response:
We could use read.table
from base R
cbind(read.table(text = as.character(as.Date(df$ALISVERIS_TARIHI,
format = "%Y%m%d")), sep="-", header = FALSE,
col.names = c("year", "month", "day")), df['a'])
year month day a
1 2019 9 1 a
2 2019 9 8 b
data
df <- structure(list(ALISVERIS_TARIHI = c("20190901", "20190908"),
a = c("a", "b")), class = "data.frame", row.names = c(NA,
-2L))