In the R programming language, what exactly means the meaning
'['
which serves as a parameter to sapply () and lapply () in the following part of the code:
dd <- data.frame(
A = c(1L, 2L, 3L),
B = c(4L, 5L, 6L),
C = c("X1=7;X2=8;X3=9",
"X1=13;X2=14",
"X1=5;X2=1;X3=8")
)
namev <- function(x) {
a <- strsplit(x,"=")
setNames(sapply(a,'[',2), sapply(a,'[',1))
}
vv <- lapply(strsplit(as.character(dd$C),";"), namev)
nm <- unique(unlist(sapply(vv, names)))
nv <- do.call(rbind, lapply(vv, '[', nm))
dd $ C [1] X1 = 7; X2 = 8; X3 = 9 X1 ;; X1 = 13; X2 = 14
Levels: X1 ;; X1 = 13; X2 = 14 X1 = 7; X2 = 8; X3 = 9
@Henrik The answer to the two answers is the same, but the questions are different. The question for which this has been marked as a duplicate ( Using the '[' square bracket as a function for lapply in R ) presupposes knowing that [is a function that is not obvious to us R newbies.
source
share