整个字符向量在 R 中保存为单个字符串
Entire character vector saved as a single string in R
我有一些矢量,我不小心将整个东西格式化为字符,现在我无法将其恢复为矢量。有没有办法在不进入正则表达式的情况下做到这一点?
例子
print(df[10])
[1] "c(\"1963-09-16\", \"1969-07-16\")"
print(length(df[10)))
[1] 1
你可以试试:
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 package (v2.0.1)
于 2021-12-07 创建
我有一些矢量,我不小心将整个东西格式化为字符,现在我无法将其恢复为矢量。有没有办法在不进入正则表达式的情况下做到这一点?
例子
print(df[10])
[1] "c(\"1963-09-16\", \"1969-07-16\")"
print(length(df[10)))
[1] 1
你可以试试:
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 package (v2.0.1)
于 2021-12-07 创建