[R] match and new columns

Val valkremk at gmail.com
Wed Dec 13 22:36:18 CET 2017


Hi all,

I have a data frame
tdat <- read.table(textConnection("A B C Y
A12 B03 C04 0.70
A23 B05 C06 0.05
A14 B06 C07 1.20
A25 A23 A12 3.51
A16 A25 A14 2,16"),header = TRUE)

I want match tdat$B with tdat$A and populate the  column   values of tdat$A
( col A and Col B) in the newly created columns (col D and col  E).  please
find my attempt and the desired output below

Desired output
A B C Y  D E
A12 B03 C04 0.70  0  0
A23 B05 C06 0.05  0  0
A14 B06 C07 1.20  0  0
A25 A23 A12 3.51 B05 C06
A16 A25 A14 2,16 A23 A12

my attempt,

tdat$D <- 0
tdat$E <- 0

if(tdat$B %in% tdat$A)
  {
  tdat$D <- tdat$A[tdat$B]
  tdat$E <- tdat$A[tdat$C]
}
 but did not work.

Thank you in advance

	[[alternative HTML version deleted]]



More information about the R-help mailing list