r - How to replace a value in multiple columns based on criteria? -


I will assign all the values ​​to 99 with NA Change the record where art == '999' only in the column L1: L8 . I know how to do this for columns at a time, but I want to do it for all the columns in a command more efficiently.

Sample data:

  df & lt; - Structure (list (KARTA = c ("02C2H", "02C2H", "02C2H", "02C2H", "02C2H", "02C2H", "02C2H", "02C2H", "02C2H", "02C2H", " 02C2H "," 02C7H "," 02C7H "," 02C7H "," 02C7H "," 02C7H "," 02C7H "," 02C7H "," 02C7H "," 02C7H "," 02C7H "," 02C7H "," 02C7H " , "02C7H", "02C7H"), YEAR = C (1997 L, 1999 L, 2000 L, 2001 L, 2002 L, 2003 L, 2005 L, 2006 L, 2007 L, 2008 L, 200 L, 2010 L , 2011 L, 1997 L, 1998 L, 2000 L, 2001 L, 2002 L, 2004 L, 2006 L, 2008 L, 200 L, 2010 L, 2011 L), ART = C ("999" , "999", "100", "100", "100", "999", "999", "999", "999", "999", "999", "999", "999" "999", "999", "999", "999", "999", "999", "999", 99, 99, 99, 99, 99, 99, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 99, 99 99, 99 99, 10, 99, 99, 99, 99), L2 = C (99, 99, 99, 99, 99, 10, 10, 10, 10, 99, 99, 99, 99, 99, 10, 9, 10, 10, 10, 10, 10), L3 = C (99, 99, 99, 99, 99 (99, 7, 10, 10, 10, 99, 99, 99, 99, 99, 10, 10, 10, 10, 10, 10, 10), L4 = C (99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 5, 8, 99, 99, 99, 99, 10, 10, 10, 10, 10, 10, 10, 10, 99, 99, 99, 99, 10, 10, 10, 10, 10, 10, 99, NA, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 10, 10, 7, 7, 10, 8), L6 = C (99, 99, 99, 99, 99, 8, 10, 10, 10, 10, 10, 10, 10, 99, 99, 99, 99, 99, 10, 10 10, 10, 10, 10, 9, 10, 10, 10, 10, 10), L7 = C (99, 99, 99, 99, 99, 99, 99, 8 , 99, 99, 99, 99, 99, 99, 10, 10, 10, 10, 10, 10, 10), L8 = C (99, 99, 99, 99, 99, 10, 10, 10, 10 , 10, 10, 10, 10, 99, 99, 99, 99, 99, 10, 10, 6, 10, 10, 10, 10)). Name = C ("Actor", "Year", "ART", "L1", "L2", "L3", "L4", "L5", "L6", "L7" , "L8"), row.Name = C (161008 L, 161009 L, 161010 L, 161011 L, 161012 L, 87055 L, 106223 L, 128072 L, 160 9 L, 172583 L, 208774 L, 45 L, 227 9 L L, 161013 L, 161014 L, 161015 L, 161016 L, 161017 L, 71813 L, 89034 L, 13 9 633 L, 181266 L, 208838 L, 97 L, 2259 9 L, Class = "Data. Fram"   

Examinations How to replace values ​​in a column ('L1') e

  df [which (df $ ART == '999' & amp; Df $ L1 == '99'),] & lt; -  

The example data in your is always 99, but I It seems that it is not always the case and you want to change only those lines with 99 with n where ART == 999 This can be done:

  df $ ART [3: 5] & lt; -100 # to give some difference DF [DF $] ART == 999, 4:10] [DF $ ART == 994: 10] == 99]  - NA Carda Year ART L1 L2 L3 L4 L5 L6 L7 L8 L8 161008 02 C-2H 1997 99 9 NA NA NA NA NA NA NA 99 161009 02 C2H 1999 99 9 NA NA NA NA NA NA NA 99 161010 02C2H 2000 100 99 99 99 99 99 99 99 99 99 161011 02 C2H 2001 100 99 99 99 99 99 99 99 99 99 161012 02 CHH 2002 100 99 99 99 99 99 99 99 99 87055 02 C2H 2003 99 9 10 10 7 10 5 8 10 10 ...    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -