> X1 <- data.frame(A=c("a1","a2","a3","a4"))
> X1
A
1 a1
2 a2
3 a3
4 a4
> y <- data.frame(A=c("b1", " ", " ", "b2"," ", "b3"), B=c("c1","c2","c3","c4","X1","c5"))
> y
A B
1 b1 c1
2 c2
3 c3
4 b2 c4
5 X1
6 b3 c5
I want to do three things in table y.
(1) replace X1 in column B of the table y with the entries (i.e. a1…a4) in the Table X1.
(2) fill all empty spaces in column A of table y.
(3) add new column C in table y and fill all with the number “1”
The new table y is expected to be as follows:
A B C
1 b1 c1 1
2 b1 c2 1
3 b1 c3 1
4 b2 c4 1
5 b2 a1 1
6 b2 a2 1
7 b2 a3 1
8 b2 a4 1
9 b3 c5 1
Could you mind to suggest effective way of doing this, I have a large number of X1 table, not just one. I have to match the names of these tables with column B of table y. I could do it one-by-one, but I am confident that there are smarter way of doing this. Pls kindly help.
To fill out the A column:
To add the C column (come on, you could’ve found this yourself!):
To expand the B column:
If the order is not important:
If the order is important:
Note that this is untested, but the ideas are there.