Skip to content
Prev 360477 / 398503 Next

Create a new variable and concatenation inside a "for" loop

"c" an extremely commonly-used function. Functions are first-class objects that occupy the same namespaces that variables do, so they can obscure each other. In short, don't use variables called "c" (R is case sensitive, so "C" has no such problem).

Wherever possible, avoid incremental concatenation like the plague. If you feel you must use it, at least concatenate in lists and then use functions like unlist, do.call, or pre-allocate vectors or matrix-like objects with unuseful values like NA and then overwrite each element in the vector or matrix-type object in a loop like your first one.