I have this df1:
A B C
1 2 3
5 7 9
where A B C
are columns names.
I have another df2 with one column:
A
1
2
3
4
I would like to append df2 for each column of df1, creating this final dataframe:
A B C
1 2 3
5 7 9
1 1 1
2 2 2
3 3 3
4 4 4
is it possible to do it?
We can replicate
df2
for the number of columns ofdf1
, unname it, thenrbind
it.Data:
Data:
Solution:
Result:
I just love R, here is yet another
Base R
solution but withmapply
:Result:
Note:
No need to deal with colnames like almost all the other solutions... The key to why this works is that "
mapply
calls FUN for the values of ... [each element] (re-cycled to the length of the longest...[element]" (See?mapply
). In other words,df2$A
is recycled to however many columnsdf1
has.Data: