I was wondering how to turn my in
put into my desired out
put?
As you can see, unlist()
adds a .
so only the .
by unlist()
must be removed not ANY other .
and replaced with /
.
in <- list(a.study = c(scale.1 = TRUE, outcome = FALSE), scale.1 = c(a.study = FALSE, outcome = FALSE))
out <- unlist(Filter(length,lapply(names(in),function(i) names(which(unlist(in[i]) == TRUE)))))
# [1] "a.study.scale.1" # Current output
desired_output <- c("a.study/scale.1") # Desired output
CodePudding user response:
Changing the setup a bit:
unlist(Filter(
length,
lapply(
names(input),
\(i) {
k <- which(input[[i]])
if (length(k)) paste0(i, '/', names(input[[i]])[k])
}
)
))
[1] "a.study/scale.1"
CodePudding user response:
Something like the following?
library(tidyverse)
inp <- list(a.study = c(scale.1 = TRUE, outcome = FALSE), scale.1 = c(a.study = FALSE, outcome = FALSE))
out <- unlist(Filter(length,lapply(names(inp),function(i) names(which(unlist(inp[i]) == TRUE)))))
out %>%
str_replace("(?<=y)\\.(?=s)", "\\/")
#> [1] "a.study/scale.1"