r - Create a new data frame column based on another column -


I have a data frame with two columns, and want to create a third column that would be essentially a boolean or No. There is a set set of values ​​specified in column 2 ("Honda Ford", "Honda Ford", "BMW", "Toyota | Chevy | Ford", "Toyota | Chevy | Ford | Honda"))

The first thing I removed was to remove it from each string in the second column, and the third call

  Library (stringer) g = str_replace_all (f $ car, "[^ [: alnum:]]", "" If you create another column, then there will be a boolean, if 1 There is a common car in it, and 0 if I want to do now, F $ make = C (g) F   

There is no normal car in it < ("BMW", "Lexus", "Acura") P> I have tried some things, including the stringer package and the following output Includes ifelse to produce.

  name of the car generally does 1 John Honda | Ford Honda Ford 1 2 Sarah BMW BMW 3 3 David Toyota | Chevy Ford Toyota Chevy Ford 1 4 Chad Toyota | Chevy Ford | Honda Toyota Chevy Ford Honda 1   

Since both of these are normal and unusual, the car should be made common by making an entry as unusual, and that row will have value in the normal column of 0 Should take. So if the entry was both BMW and Ford, the entry should take 0 in the normal column.

Can anyone help in this task

Oh, and here I tried to do with the Stringer package but it does not work. Common_match & amp; nbsp;

  normal = C ("Honda", "Ford", "Toyota" CVC "," Chevy ") not_common = c (" BMW "," Lexus " Lt; - str_c (normal) not_match <- str_c (not_common) main <- function (df) {f $ new_make> - str_detect (f $ make, common_match) df} main (f)   

Thank you!

Another method and one comparison < Pre> f2 & lt; -f [rep (1: 4,50000),] system.time ({v & lt; - sapply (f2 $ make, strsplit, "") sapply (v, function (x ) Max (% x in% x)) * max (in% x% x))) User system has passed 7.94 0.01 8.00 system.time (Sapply (f2 $ car, function (x) ifelse (length (grep (" Bmdub Leu | Lexus | Acura ", x)) gt; 0,0,1)) user system has passed 28.72 0.04 28.87

Comments