Simon Pickett
2008-Dec-11 18:41 UTC
[R] help: programming loop, binding columns between data sets
Hi,
I often have the problem of combining data sets of different lengths
together.
Simple example: I have data frame "a", with two columns "C1"
and "C2"
and another data frame "b" with only one column "V1".
Data frame "b" is much bigger than "a", but "C1"
of "a" has the same
levels as "V1" of "b". (so in other words there are multiple
instances
of a$C1 in b$V1)
I wish to paste a$C2 into a new column in "b", where a$C1 == b$V1.
I have always done it this way...
for (i in 1:dim(a)[1])
{b[b$V1 == a$C1[i],c("V2")]=a[i,c("C2")]}
However,
1. It is very slow
2. It is unreliable (in that for no reason at all, I often get NA's in
the new column of B)
-This usually happens when the code is within a loop, or I have to paste
multiple columns from "a" across to "b" all at once. In this
case I
often have to paste each column one at a time which takes forever.
I often am dealing with very large data sets. I am using R 2.1.1 on
Windows Vista.
Can anyone suggest a faster/more reliable alternative alternative
please? Needless to say I am a programming novice.
Thanks in advance,
Simon Pickett.