1

I have a column with big character strings like this:

"[{'tipoTeste': 'RT-PCR', 'estadoTeste': 'Concluído',
'dataColetaTeste': {'__type': 'Date', 'iso': '2021-12-30T03:00:00.000Z'}, 
'resultadoTeste': 'Detectável', 'loteTeste': None, 
'fabricanteTeste': None, 'codigoTipoTeste': '1', 'codigoEstadoTeste': '3', 
'codigoResultadoTeste': '1', 'codigoFabricanteTeste': None}]"

And i want to create another variable called Date with the date information inside this huge string, in this case is 2021-12-30

Im not managing to grep this date information for all rows....

1
  • does your string contain \n ie linebreaks? Commented Jun 10, 2022 at 14:50

2 Answers 2

1

This would work:

library(stringr)
str_extract_all(str, "[0-9]{4}-[0-9]{2}-[0-9]{2}")
[[1]]
[1] "2021-12-30"
Sign up to request clarification or add additional context in comments.

Comments

1

We could use parse_date directly on the string

library(parsedate)
> as.Date(parse_date(str1))
[1] "2021-12-30"

data

str1 <- "[{'tipoTeste': 'RT-PCR', 'estadoTeste': 'Concluído',
'dataColetaTeste': {'__type': 'Date', 'iso': '2021-12-30T03:00:00.000Z'}, 
'resultadoTeste': 'Detectável', 'loteTeste': None, 
'fabricanteTeste': None, 'codigoTipoTeste': '1', 'codigoEstadoTeste': '3', 
'codigoResultadoTeste': '1', 'codigoFabricanteTeste': None}]"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.