Question

I'd like to match the contents of a couple of columns of various data frames - the cells are regular expressions, and I'd like to use these over a loop to match so as to extract an associated value. For example:

head(DET_Data)
Gm.       Date  Tm H.A Opp Result  R RA Inn Record          Win        Loss   Save D.N  OU Total
2   2 2014-04-02 DET     KCR      W  2  1  10    2-0 Alburquerque     Collins          D 7.5     

I'd like to match the batters in the Win column with the pitchers record, which is in a separate data frame:

          Pitcher Age  Tm Lg W L  W.L.  ERA  G GS GF CG SHO SV   IP  H R ER HR BB IBB SO HBP BK WP BF ERA.  FIP  WHIP  H9 HR9 BB9  SO9 SO.W
6 Al Alburquerque  28 DET AL 1 0 1.000 4.11 19  0  5  0   0  1 15.1 16 7  7  2  4   0 20   0  0  0 66  105 2.97 1.304 9.4 1.2 2.3 11.7 5.00

I'd like to be able to read the string contents, and use that to match with the pitcher name and so on. Is there a straight forward way of doing so ?

Was it helpful?

Solution

The grep function should help you :

x<-c("you","shall","not","pass")
grep("you",x)
[1] 1
grep("sha",x)
[1] 2
grep("314",x)
integer(0)

As you can see, the match has not to be the exact same to be found.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top