0

我有一些向量,我不小心将整个事物格式化为字符,现在我无法将其恢复为向量。有没有办法在不进入正则表达式的情况下做到这一点?

例子

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

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

1 回答 1

0

你可以试试:

library(tidyverse)

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

或者来自 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

reprex 包于 2021-12-07 创建(v2.0.1)

于 2021-12-07T20:49:54.020 回答