为什么在这里 deparse return 一个长度为 2 的向量?
Why does deparse return a vector of length two here?
我需要使用 deparse(...)
在 R 中进行解析,这在所有情况下都返回了一个长度为 1 的向量。但是对于以下内容,我得到了一个长度为二的向量:
> deparse(factor(All_cause_death, levels= c('Dead', 'Alive')) ~ Age_in_years + Systolic_Blood_pressure, backtick= TRUE)
[1] "factor(All_cause_death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + "
[2] " Systolic_Blood_pressure"
如果我稍微缩短第一个变量名,我确实会得到长度为 1 的预期输出:
> deparse(factor(Death, levels= c('Dead', 'Alive')) ~ Age_in_years + Systolic_Blood_pressure, backtick= TRUE)
[1] "factor(Death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + Systolic_Blood_pressure"
在上面的代码中,我通过将“All_cause_death”更改为“Death”,将输入缩短了 10 个元素。有趣的是,如果我在输入末尾将输入缩短 10 个元素(或更多),我不会得到长度为 1 的向量:
> deparse(factor(All_cause_death, levels= c('Dead', 'Alive')) ~ Age_in_years + Systolic_, backtick= TRUE)
[1] "factor(All_cause_death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + "
[2] " Systolic_"
为什么会发生这种情况,如何为第一个代码获得长度为 1 的向量?
如果超过 60 个字符,deparse
将换行。它具有 width.cutoff
参数,如果您不希望这种情况发生,您可以将其设置为更长的长度:
deparse(factor(All_cause_death, levels= c('Dead', 'Alive')) ~ Age_in_years+ Systolic_Blood_pressure, backtick= TRUE, width.cutoff = 200)
#> [1] "factor(All_cause_death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + Systolic_Blood_pressure"
来自 deparse
文档:
width.cutoff
integer in [20, 500] determining the cutoff (in bytes) at which line-breaking is tried.
您也可以使用 deparse1
而不是 deparse
,这实际上只是将最大值 width.cutoff
默认设置为 500
deparse1(factor(All_cause_death, levels= c('Dead', 'Alive')) ~ Age_in_years+ Systolic_Blood_pressure, backtick= TRUE)
#> [1] "factor(All_cause_death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + Systolic_Blood_pressure"
来自 deparse1
的文档:
deparse1() is a simple utility added in R 4.0.0 to ensure a string result (character vector of length one), typically used in name construction, as deparse1(substitute(.)).
我需要使用 deparse(...)
在 R 中进行解析,这在所有情况下都返回了一个长度为 1 的向量。但是对于以下内容,我得到了一个长度为二的向量:
> deparse(factor(All_cause_death, levels= c('Dead', 'Alive')) ~ Age_in_years + Systolic_Blood_pressure, backtick= TRUE)
[1] "factor(All_cause_death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + "
[2] " Systolic_Blood_pressure"
如果我稍微缩短第一个变量名,我确实会得到长度为 1 的预期输出:
> deparse(factor(Death, levels= c('Dead', 'Alive')) ~ Age_in_years + Systolic_Blood_pressure, backtick= TRUE)
[1] "factor(Death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + Systolic_Blood_pressure"
在上面的代码中,我通过将“All_cause_death”更改为“Death”,将输入缩短了 10 个元素。有趣的是,如果我在输入末尾将输入缩短 10 个元素(或更多),我不会得到长度为 1 的向量:
> deparse(factor(All_cause_death, levels= c('Dead', 'Alive')) ~ Age_in_years + Systolic_, backtick= TRUE)
[1] "factor(All_cause_death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + "
[2] " Systolic_"
为什么会发生这种情况,如何为第一个代码获得长度为 1 的向量?
deparse
将换行。它具有 width.cutoff
参数,如果您不希望这种情况发生,您可以将其设置为更长的长度:
deparse(factor(All_cause_death, levels= c('Dead', 'Alive')) ~ Age_in_years+ Systolic_Blood_pressure, backtick= TRUE, width.cutoff = 200)
#> [1] "factor(All_cause_death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + Systolic_Blood_pressure"
来自 deparse
文档:
width.cutoff
integer in [20, 500] determining the cutoff (in bytes) at which line-breaking is tried.
您也可以使用 deparse1
而不是 deparse
,这实际上只是将最大值 width.cutoff
默认设置为 500
deparse1(factor(All_cause_death, levels= c('Dead', 'Alive')) ~ Age_in_years+ Systolic_Blood_pressure, backtick= TRUE)
#> [1] "factor(All_cause_death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + Systolic_Blood_pressure"
来自 deparse1
的文档:
deparse1() is a simple utility added in R 4.0.0 to ensure a string result (character vector of length one), typically used in name construction, as deparse1(substitute(.)).