LuisMoncayo
LuisMoncayo

Reputation: 125

Remove a sequence in a character in R

I have the following character in a data.frame:

b <- "http://datos.labcd.mx/dataset/5b18cc1e-d2f2-46b0-bf2c-e699ae2af713/resource/e265a46f-7a9f-4a30-ae0d-d5937fff17c1/download/201003.csv"

I just want to extract the number 201003.

How should I do that?

Upvotes: 0

Views: 146

Answers (1)

Sowmya S. Manian
Sowmya S. Manian

Reputation: 3843

b <- "http://datos.labcd.mx/dataset/5b18cc1e-d2f2-46b0-bf2c-e699ae2af713/resource/e265a46f-7a9f-4a30-ae0d-d5937fff17c1/download/201003.csv"

Try this on 'b':

file_name <- basename(b)
file_name
# [1] "201003.csv"
number <- strsplit(file_name, "\\.")[[1]]
number
# [1] "201003"  "csv"
number = as.numeric(number[1])
number
# [1] 201003

Hope this helped.

Upvotes: 1

Related Questions