In the programming language R what, precisely, is the meaning of
'['
which serves as a parameter to sapply() and lapply() in the following portion of 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)))
#extract data from all rows for every column
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 are the same but the questions are different. The question for which this has been marked duplicate (Using '[' square bracket as a function for lapply in R ) presupposes a knowledge that [ is a function which is not self evident to us R newbies.