r - How do I replace NA in one column if another column's value is greater than 0? -
I have 8 census lines ( Sample data: sample code for a single column (note I'm looking for an efficient solution to all eight columns): Result: L1: L8 ) currently in some records
When they are placed after
0 instead of NA , I will enter
NA in each column (
L1: L8 )
0 when the respective attempts column (
EFFORT_L1: EFFORT_L8 ) has a value greater than 0 (which means that they are censored).
df < -structure (list (KARTA = c ( "02C2H", "02C2H", "02C2H", "02C2H", "02C2H", "02C2H"), year = c (1997L, 1997L, 1997L, 1997L, 1997L, 1997L) , ART = c ("009", "031", "012", "057", "065", "073"), L1 = C (NA, NA, NA, NA, 2, NA), L2 = C (NA, NA, 7, NA, 3, NA), L3 = C (NA, NA, NA, NA, NA), L4 = C (NA, NA, NA, NA, 1, NA) L5 = C (NA, NA, NA, NA 1, NA), L 6 = C (NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), L7 = C (NA, NA, NA 1, NA 1), L 8 = C (NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), EFFORT_L1 = c (10, 10, 10, 10, 10, 10), EFFORT_L2 = c (10, 10, 10, 10 , 10, 10), EFFORT_L3 = c (9.625, 9.625, 9.625, 9.625, 9.625, 9, 625), EFFR Ititii = C (10, 10, 10, 10, 10, 10), Iffartiartiuar 5 = C (9.125, 9.125, 9.125, 9 .125, 9.125, 9.125), EFFORT_L6 = c (9.75, 9.75, 9.75, 9.75, 9.75 , 9.75), EFFORT_L7 = c (9.75, 9.75, 9.75, 9.75, 9.75, 9.75), EFFORT_L8 = c (10, 10, 10, 10, 10, 10), Kul_rut_afort = C (78.25, 78.25, 78.25, 78.25, 78.25, 78.25)). Name = C ("Actor", "Year", "ART", "L1", "L2", "L3", "L4", "L5", "L6", "L7", " L8 "," EFFORT_L1 "," EFFORT_L2 "," EFFORT_L3 "," EFFORT_L4 "," EFFORT_L5 "," EFFORT_L6 "," EFFORT_L7 "," EFFORT_L8 "," Total_Route_Effort "), Panktiknam = C (NA, 6 L ) class = "Detakfrem")
df [is.na (df [, "L1"]) & amp; Df [, "EFFORT_L1"] & gt; 0, "L1"] & lt; - 0
df [paste0 ("L", 1: 8) ] [Is.na (df [paste0 ("L", 1: 8)]) and; Df [paste0 ("EFFORT_L", 1: 8)]> 0] & lt; - 0
& gt; df doers year ART L1 2 L3 L4 L5 L6 L7 L8 EFFORT_L1 EFFORT_L2 1 02C2H 1997 009 0 0 0 0 0 0 0 0 10 10 2 02C2H 1997 031 0 0 0 0 0 0 0 0 10 10 3 02C2H 1997 012 0 7 0 0 0 0 0 10 10 4 02 C 2 H 1997 057 0 0 0 0 0 0 0 0 10 10 5 02 C 2 H 1 99 0 065 2 3 1 1 1 0 0 10 10 6 02 C 2 H 1997 073 0 0 0 0 0 1 0 10 10 EF Forne 0 L3 Iffartitiel 4 EFFORT_L5 EFFORT_L6 EFFORT_L7 EFFORT_L8 1 9,625 10 9,125 9,75 9,75 10 2 9,625 10 9,125 9,75 9,75 10 3 9,625 10 9,125 9,75 9, 75 10 4 9.625 10 9.125 9.75 9.75 10 5 9.625 10 9.125 9.75 9.75 10 6 9.625 10 9.125 9.75 9.75 10 Total_rut_frt 1 78.25 2 78.25 3 78.25 4 78.25 5 78.25 6 78.25
Comments
Post a Comment