我开发了一个函数,它将文本重新表述为线性模型公式中的可用表达式。然而,当这些公式太长时,它们就会分裂。因此,我试图连接那些被分割的字符串。
与一些示例数据:
[[4]]$X7
[1] "lifespan ~ exposure + danger + body + brain"
[[4]]$X8
[1] "lifespan ~ danger + body + brain + nondream"
[[4]]$X9
[1] "lifespan ~ body + brain + nondream + dream"
[[8]]$X8
[1] "lifespan ~ danger + body + brain + nondream + dream + sleep + "
[2] " gestation + predation"
[[8]]$X9
[1] "lifespan ~ body + brain + nondream + dream + sleep + gestation + "
[2] " predation + exposure"
lapply(formula_pred, lapply(x)if(length(x)>= 2){paste(x, collapse="")})
但是,这只是将列表中的所有字符串粘贴到每个列表中的一个字符串中。
例如,我得到这样的内容:
[[1]]
[1] "lifespan ~ brainlifespan ~ nondreamlifespan ~ dreamlifespan ~ sleeplifespan ~ gestationlifespan ~ predationlifespan ~ exposurelifespan ~ dangerlifespan ~ body"
预期输出:
[[4]]$X7
[1] "lifespan ~ exposure + danger + body + brain"
[[4]]$X8
[1] "lifespan ~ danger + body + brain + nondream"
[[4]]$X9
[1] "lifespan ~ body + brain + nondream + dream"
[[8]]$X8
[1] "lifespan ~ danger + body + brain + nondream + dream + sleep + gestation + predation"
[[8]]$X9
[1] "lifespan ~ body + brain + nondream + dream + sleep + gestation + predation + exposure"
复制代码:
list(list(X1 = "lifespan ~ brain", X2 = "lifespan ~ nondream",
X3 = "lifespan ~ dream", X4 = "lifespan ~ sleep", X5 = "lifespan ~ gestation",
X6 = "lifespan ~ predation", X7 = "lifespan ~ exposure",
X8 = "lifespan ~ danger", X9 = "lifespan ~ body"), list(X1 = c("lifespan ~ brain + nondream + dream + sleep + gestation + predation + ",
" exposure + danger"), X2 = c("lifespan ~ nondream + dream + sleep + gestation + predation + ",
" exposure + danger + body"), X3 = c("lifespan ~ dream + sleep + gestation + predation + exposure + ",
" danger + body + brain"), X4 = c("lifespan ~ sleep + gestation + predation + exposure + danger + ",
" body + brain + nondream"), X5 = c("lifespan ~ gestation + predation + exposure + danger + body + ",
" brain + nondream + dream"), X6 = c("lifespan ~ predation + exposure + danger + body + brain + nondream + ",
" dream + sleep"), X7 = c("lifespan ~ exposure + danger + body + brain + nondream + dream + ",
" sleep + gestation"), X8 = c("lifespan ~ danger + body + brain + nondream + dream + sleep + ",
" gestation + predation"), X9 = c("lifespan ~ body + brain + nondream + dream + sleep + gestation + ",
" predation + exposure")))
这有帮助吗?
library(dplyr)
as.data.frame(dt) %>%
mutate(across(matches("\.\d"), ~paste0(trimws(.), collapse = " ")))
X1 X2 X3 X4 X5 X6
1 lifespan ~ brain lifespan ~ nondream lifespan ~ dream lifespan ~ sleep lifespan ~ gestation lifespan ~ predation
2 lifespan ~ brain lifespan ~ nondream lifespan ~ dream lifespan ~ sleep lifespan ~ gestation lifespan ~ predation
X7 X8 X9
1 lifespan ~ exposure lifespan ~ danger lifespan ~ body
2 lifespan ~ exposure lifespan ~ danger lifespan ~ body
X1.1
1 lifespan ~ brain + nondream + dream + sleep + gestation + predation + exposure + danger
2 lifespan ~ brain + nondream + dream + sleep + gestation + predation + exposure + danger
X2.1
1 lifespan ~ nondream + dream + sleep + gestation + predation + exposure + danger + body
2 lifespan ~ nondream + dream + sleep + gestation + predation + exposure + danger + body
X3.1
1 lifespan ~ dream + sleep + gestation + predation + exposure + danger + body + brain
2 lifespan ~ dream + sleep + gestation + predation + exposure + danger + body + brain
X4.1
1 lifespan ~ sleep + gestation + predation + exposure + danger + body + brain + nondream
2 lifespan ~ sleep + gestation + predation + exposure + danger + body + brain + nondream
X5.1
1 lifespan ~ gestation + predation + exposure + danger + body + brain + nondream + dream
2 lifespan ~ gestation + predation + exposure + danger + body + brain + nondream + dream
X6.1
1 lifespan ~ predation + exposure + danger + body + brain + nondream + dream + sleep
2 lifespan ~ predation + exposure + danger + body + brain + nondream + dream + sleep
X7.1
1 lifespan ~ exposure + danger + body + brain + nondream + dream + sleep + gestation
2 lifespan ~ exposure + danger + body + brain + nondream + dream + sleep + gestation
X8.1
1 lifespan ~ danger + body + brain + nondream + dream + sleep + gestation + predation
2 lifespan ~ danger + body + brain + nondream + dream + sleep + gestation + predation
X9.1
1 lifespan ~ body + brain + nondream + dream + sleep + gestation + predation + exposure
2 lifespan ~ body + brain + nondream + dream + sleep + gestation + predation + exposure
在OP的代码中,有一个语法问题-即它显示x
,但没有lambda函数(function(x)
或(x)
-在新版本中)。此外,它是一个嵌套列表,因此我们可能需要两个lambda函数
lapply(formula_pred, function(x)
lapply(x, function(y) paste(trimws(y), collapse = ' ')))
或者如果我们想使用if/else
(实际上不需要,因为它给出相同的结果)
lapply(formula_pred, function(x)
lapply(x, function(y)
if(length(y) >=2) paste(trimws(y), collapse = '') else y))