Home > OS >  Extracting information from string and creating new variable
Extracting information from string and creating new variable

Time:06-11

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....

CodePudding user response:

This would work:

library(stringr)
str_extract_all(str, "[0-9]{4}-[0-9]{2}-[0-9]{2}")
[[1]]
[1] "2021-12-30"

CodePudding user response:

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}]"
  • Related