我需要在R中使用两个变量制作一小段文本。我有以下数据:
library(dplyr)
VAR <- c("Age", "Condition", "Condition2")
FIELDS <- c("", "Option1;Option2", "Set1;Set2")
df <- cbind(VAR, FIELDS) %>%
as_data_frame()
一个在循环中写入文本的函数:
extending <- function(x, VAR){
VARZ <- VAR[!is.na(x)]
sapply(x, function(x){
if(!is.na(x)){
VARZ <- "XXXXX"
opciones <- strsplit(x, ";")
opciones <- opciones[[1]]
opciones2 <- paste(
"From ", VARZ, " here's ", opciones,
sep=""
)
} else {
opciones2 <- ""
}
opciones2
})
}
当我使用带有两个变量的函数时:
extending(df$FIELDS, df$VAR)
结果如下:
# result 1
[[1]]
[1] "From XXXXX here's "
$`Option1;Option2`
[1] "From XXXXX here's Option1" "From XXXXX here's Option2"
$`Set1;Set2`
[1] "From XXXXX here's Set1" "From XXXXX here's Set2"
我想得的是以下内容:
# result 2
[[1]]
[1] "From Age here's "
$`Option1;Option2`
[1] "From Condition here's Option1" "From Condition here's Option2"
$`Set1;Set2`
[1] "From Condition2 here's Set1" "From Condition2 here's Set2"
但如果我禁用行VARZ <- "XXXXX"
,我会得到一些完全不同的东西:
# result 3
Option1;Option2 Set1;Set2
[1,] "From Age here's " "From Age here's Option1" "From Age here's Set1"
[2,] "From Condition here's " "From Condition here's Option2" "From Condition here's Set2"
[3,] "From Condition2 here's " "From Condition2 here's Option1" "From Condition2 here's Set1"
我尝试了一些变化,但最终得到了更奇怪的结果,并且不太了解我正在做的事情。
这是一种编写循环的方法,该循环在&#34;适当的&#34;中重用每个变量。在# result 2
块中编写文本的方法?
答案 0 :(得分:2)
这是一个快速的基础代码:
Raycaster
当然,只有当您对名称不感兴趣时才能缩短代码:您可以在以后设置
const raycaster = new THREE.Raycaster();
markers.forEach(marker => {
raycaster.set(marker.getWorldPosition(), new THREE.Vector3(0, -1, 0).normalize());
let intersects = raycaster.intersectObjects([map]);
if(intersects.length) {
marker.position.y -= intersects[0].distance;
}
});