Search code examples
rnested-tibble

Entire character vector saved as a single string in R


I have some vectors I accidentally formatted the entire thing as character and now I can't revert it back into it being a vector. Is there a way to do this without going into regex?

Example

print(df[10])
[1] "c(\"1963-09-16\", \"1969-07-16\")"

print(length(df[10)))
[1] 1

Solution

  • You can try:

    library(tidyverse)
    
    eval(parse(text = "c(\"1963-09-16\", \"1969-07-16\")") )
    #> [1] "1963-09-16" "1969-07-16"
    

    Or from a df

    df <- data.frame(dates = rep("c(\"1963-09-16\", \"1969-07-16\")", 5))
    
    summarise(df, dates = map(dates, function(x) eval(parse(text = x))) %>%
      reduce(c))
    #>         dates
    #> 1  1963-09-16
    #> 2  1969-07-16
    #> 3  1963-09-16
    #> 4  1969-07-16
    #> 5  1963-09-16
    #> 6  1969-07-16
    #> 7  1963-09-16
    #> 8  1969-07-16
    #> 9  1963-09-16
    #> 10 1969-07-16
    

    Created on 2021-12-07 by the reprex package (v2.0.1)