D/How/CDS_2021/20210104/ASUS_0001_0001/QBA_X302LA/20201201_ABA_Window_X302LA
D/How/CDS_2021/20210106/ASUS_0002_0001/QBA_X302LA/20201204_ABA_Window_X302LA
D/How/CDS_2021/20210109/ASUS_0003_0001/QBA_X302LA/20201207_ABA_Window_X302LA
D/How/CDS_2021/20210111/ASUS_0004_0001/QBA_X302LA/20201210_ABA_Window_X302LA
D/How/CDS_2021/20210115/ASUS_0005_0001/QBA_X302LA/20201218_ABA_Window_X302LA
D/How/CDS_2021/20210117/ASUS_0007_0001/QBA_X302LA/20201228_ABA_Window_X302LA
How do I get only after No3. slash string,like
SaleDate |
---|
20210104 |
20210106 |
20210109 |
20210111 |
20210115 |
20210117 |
CodePudding user response:
Use strsplit
with sep = "/"
to split your strings between directories. Then get the fourth element using sapply
and [
:
vec = c("D/How/CDS_2021/20210104/ASUS_0001_0001/QBA_X302LA/20201201_ABA_Window_X302LA",
"D/How/CDS_2021/20210106/ASUS_0002_0001/QBA_X302LA/20201204_ABA_Window_X302LA",
"D/How/CDS_2021/20210109/ASUS_0003_0001/QBA_X302LA/20201207_ABA_Window_X302LA")
data.frame(SaleDate = sapply(strsplit(vec, '/'), `[`, 4))
SaleDate
1 20210104
2 20210106
3 20210109
CodePudding user response:
You could use word
from stringr
:
library(stringr)
word(x, 4, sep = '/')
# [1] "20210104" "20210106" "20210109"
or str_extract()
:
str_extract(x, "(?<=/)\\d (?=/)")
Data
x <- c("D/How/CDS_2021/20210104/ASUS_0001_0001/QBA_X302LA/20201201_ABA_Window_X302LA",
"D/How/CDS_2021/20210106/ASUS_0002_0001/QBA_X302LA/20201204_ABA_Window_X302LA",
"D/How/CDS_2021/20210109/ASUS_0003_0001/QBA_X302LA/20201207_ABA_Window_X302LA")
CodePudding user response:
- We can use
gsub
y <- gsub(".*/(\\d*)/.*" , "\\1" , x)
- Output
[1] "20210104" "20210106" "20210109" "20210111" "20210115"
[6] "20210117"
and if you want to transform the result to a date object you can use
as.Date(y , "%Y%m%d")
[1] "2021-01-04" "2021-01-06" "2021-01-09" "2021-01-11"
[5] "2021-01-15" "2021-01-17"
- Data
x <- c("D/How/CDS_2021/20210104/ASUS_0001_0001/QBA_X302LA/20201201_ABA_Window_X302LA",
"D/How/CDS_2021/20210106/ASUS_0002_0001/QBA_X302LA/20201204_ABA_Window_X302LA",
"D/How/CDS_2021/20210109/ASUS_0003_0001/QBA_X302LA/20201207_ABA_Window_X302LA",
"D/How/CDS_2021/20210111/ASUS_0004_0001/QBA_X302LA/20201210_ABA_Window_X302LA",
"D/How/CDS_2021/20210115/ASUS_0005_0001/QBA_X302LA/20201218_ABA_Window_X302LA",
"D/How/CDS_2021/20210117/ASUS_0007_0001/QBA_X302LA/20201228_ABA_Window_X302LA"
)