Is there a way to handle the first lapply
element differently? or prepend it to the lapply
result on the subsequent elements?
For example, I have the following:
mapping <- c("a", "b", "c")
# this doesn't matter here
#names(mapping) <- c("aa", "bb", "cc")
xmlvec <- c('<item a="1" c="2" />',
'<item b="3" c="4" />',
'<item a="5" b="6" c="7"/>')
df <- as.data.frame(do.call(
rbind,
lapply(xml_children(read_xml(paste("<xml>", paste(xmlvec, collapse=""), "</xml>"))),
function(x) {
y <- xml_attrs(x)[mapping]
if (any(is.na(names(y)))) {
y <- y[-which(is.na(names(y)))]
}
y[setdiff(mapping, names(y))] <- NA
y[order(factor(names(y), levels=mapping))]
}
)
), stringsAsFactors = FALSE)
df
I'd like the first lapply
element to be handled in such a way and all the subsequent ones only like this:
df <- as.data.frame(do.call(
rbind,
lapply(xml_children(read_xml(paste("<xml>", paste(xmlvec, collapse=""), "</xml>"))),
function(x) {
xml_attrs(x)[mapping]
}
)
), stringsAsFactors = FALSE)
df
The expected output for both cases (doing the bigger processing first or for every element) must be:
a b c
1 1 <NA> 2
2 <NA> 3 4
3 5 6 7