This is a toy example of something I always end up writing a loop for. I’m having trouble figuring out the one liner. I’m sure I’ve seen it, but it didn’t stick.
smallFrame <- data.frame(colA = c('A', 'B', 'C' ,'D'), colB = c(1,1,1,1))
someList <- list(A=20, B=30, C=40, D=50)
for(letter in names(someList)){
smallFrame[smallFrame$colA==letter, 'newColumn'] <- someList[[letter]]
}
how do I do the loop in one line? This won’t do it.
lapply(names(someList), function(x) {smallFrame[smallFrame$colA==x, 'newColumn'] <- someList[[x]]})
This is a simple merge, if you reshape your
smallListappropriatelyor, if you are really keen on assigning within smallFrame and efficiently, use
data.table