I need to make a new data frame (col.3
) using only the occurrences in a previous column (col.1
) that correspond to unique values in another column (col.2
) in an existing data frame.
I need this:
df1
col.1 col.2
1 1
1 3
1 7
1 7
2 12
2 14
2 14
2 14
df2
col.3
1
1
1
2
2
I have tried this:
new.col <- cbind(df$col.1[unique(df$col.2)])
But it gives me a column that is both too long, and which does not include the complete set of col.1
values
I suspect that plyr
has a simple solution to this, but I have not figured that (or any other solution) out.
How can I achieve my desired result? Preferably using plyr
, but base
is fine too.