Skip to main content
added 155 characters in body
Source Link
E. Chan-López
  • 31.2k
  • 3
  • 29
  • 50

Using ReplacePart and Cases as follows:

a = {{{1, 0}, {1, 7}, {2, 0}, {3, 2}, {4, 70}}, 
     {{1, 0}, {1, 0}, {2, 1}, {3, 20}, {4, 7}}, 
     {{1, 0}, {1, 1}, {1, 0}, {3, 1}, {4, 0}}};

pattpatt1 = {x1_, x2_} /; x1 != 0 && x2 == 0;

pos[l_List] := Position[l, patt]patt1]

parts[l_List] /.:= {x1_Cases[l, x2_} /; x2 == 1patt1 -> n{x1, Na}, 2]

delrulespatt2 = DeleteCases[#, Rule[s1_Rule[lhs_, s2_]rhs_] /; !lhs[[2]] ListQ[s1]]!= &;1;

ReplacePart[#,rep[l_List] delrules@Thread[pos[#]:= Cases[Thread[pos[#] -> parts[#]]]parts[#]] &@l, patt2]

ReplacePart[#, rep[#]] &@a

(*{{{1, 0}, {1, 7}, {2, Na}, {3, 2}, {4, 7Na}},
   {{1, 0}, {1, Na}, {2, 1}, {3, 2Na}, {4, 7}},
   {{1, 0}, {1, 1}, {1, Na}, {3, 1}, {4, Na}}}*)

Using ReplacePart and Cases as follows:

a = {{{1, 0}, {1, 7}, {2, 0}, {3, 2}, {4, 7}}, 
     {{1, 0}, {1, 0}, {2, 1}, {3, 2}, {4, 7}}};

patt = {x1_, x2_} /; x1 != 0 && x2 == 0;

pos[l_List] := Position[l, patt] /. {x1_, x2_} /; x2 == 1 -> n

delrules = DeleteCases[#, Rule[s1_, s2_] /; ! ListQ[s1]] &;

ReplacePart[#, delrules@Thread[pos[#] -> parts[#]]] &@a

(*{{{1, 0}, {1, 7}, {2, Na}, {3, 2}, {4, 7}},
   {{1, 0}, {1, Na}, {2, 1}, {3, 2}, {4, 7}}}*)

Using ReplacePart and Cases as follows:

a = {{{1, 0}, {1, 7}, {2, 0}, {3, 2}, {4, 0}}, 
     {{1, 0}, {1, 0}, {2, 1}, {3, 0}, {4, 7}}, 
     {{1, 0}, {1, 1}, {1, 0}, {3, 1}, {4, 0}}};

patt1 = {x1_, x2_} /; x1 != 0 && x2 == 0;

pos[l_List] := Position[l, patt1]

parts[l_List] := Cases[l, patt1 -> {x1, Na}, 2]

patt2 = Rule[lhs_, rhs_] /; lhs[[2]] != 1;

rep[l_List] := Cases[Thread[pos[#] -> parts[#]] &@l, patt2]

ReplacePart[#, rep[#]] &@a

(*{{{1, 0}, {1, 7}, {2, Na}, {3, 2}, {4, Na}},
   {{1, 0}, {1, Na}, {2, 1}, {3, Na}, {4, 7}},
   {{1, 0}, {1, 1}, {1, Na}, {3, 1}, {4, Na}}}*)
Post Undeleted by E. Chan-López
added 116 characters in body
Source Link
E. Chan-López
  • 31.2k
  • 3
  • 29
  • 50

Using ReplacePart and Cases as follows:

a = {{{01, 0}, {1, 7}, {2, 0}, {3, 2}, {4, 7}}, 
     {{01, 0}, {1, 0}, {2, 1}, {3, 2}, {4, 7}}};

patt = {x1_, x2_} /; x1 != 0 && x2 == 0;

ReplacePart[#,pos[l_List] Thread[Position[#:= Position[l, patt] ->/. Cases[#{x1_, pattx2_} /; x2 == 1 -> {x1n

delrules = DeleteCases[#, Na}Rule[s1_, 2]]]s2_] /; ! ListQ[s1]] &;

ReplacePart[#, delrules@Thread[pos[#] -> parts[#]]] &@a

(*{{{01, 0}, {1, 7}, {2, Na}, {3, 2}, {4, 7}}, 
   {{01, 0}, {1, Na}, {2, 1}, {3, 2}, {4, 7}}}*)

Using ReplacePart and Cases as follows:

a = {{{0, 0}, {1, 7}, {2, 0}, {3, 2}, {4, 7}},
     {{0, 0}, {1, 0}, {2, 1}, {3, 2}, {4, 7}}};

patt = {x1_, x2_} /; x1 != 0 && x2 == 0;

ReplacePart[#, Thread[Position[#, patt] -> Cases[#, patt -> {x1, Na}, 2]]] &@a

(*{{{0, 0}, {1, 7}, {2, Na}, {3, 2}, {4, 7}}, 
 {{0, 0}, {1, Na}, {2, 1}, {3, 2}, {4, 7}}}*)

Using ReplacePart and Cases as follows:

a = {{{1, 0}, {1, 7}, {2, 0}, {3, 2}, {4, 7}}, 
     {{1, 0}, {1, 0}, {2, 1}, {3, 2}, {4, 7}}};

patt = {x1_, x2_} /; x1 != 0 && x2 == 0;

pos[l_List] := Position[l, patt] /. {x1_, x2_} /; x2 == 1 -> n

delrules = DeleteCases[#, Rule[s1_, s2_] /; ! ListQ[s1]] &;

ReplacePart[#, delrules@Thread[pos[#] -> parts[#]]] &@a

(*{{{1, 0}, {1, 7}, {2, Na}, {3, 2}, {4, 7}},
   {{1, 0}, {1, Na}, {2, 1}, {3, 2}, {4, 7}}}*)
Post Deleted by E. Chan-López
Post Undeleted by E. Chan-López
added 93 characters in body
Source Link
E. Chan-López
  • 31.2k
  • 3
  • 29
  • 50

Using ReplaceAllReplacePart and Cases as follows:

a = {{{0, 0}, {1, 7}, {2, 0}, {3, 2}, {4, 7}},
     {{0, 0}, {1, 0}, {2, 1}, {3, 2}, {4, 7}}};

a /. xpatt := {_x1_, 0x2_} /; x[[1]]x1 != 0 :&& x2 == 0;

ReplacePart[#, Thread[Position[#, patt] -> Cases[#, patt -> {x[[1]]x1, Na}, 2]]] &@a

(*{{{0, 0}, {1, 7}, {2, Na}, {3, 2}, {4, 7}}, 
 {{0, 0}, {1, Na}, {2, 1}, {3, 2}, {4, 7}}}*)

Using ReplaceAll as follows:

a = {{{0, 0}, {1, 7}, {2, 0}, {3, 2}, {4, 7}},
     {{0, 0}, {1, 0}, {2, 1}, {3, 2}, {4, 7}}};

a /. x : {_, 0} /; x[[1]] != 0 :> {x[[1]], Na}

(*{{{0, 0}, {1, 7}, {2, Na}, {3, 2}, {4, 7}}, 
 {{0, 0}, {1, Na}, {2, 1}, {3, 2}, {4, 7}}}*)

Using ReplacePart and Cases as follows:

a = {{{0, 0}, {1, 7}, {2, 0}, {3, 2}, {4, 7}},
     {{0, 0}, {1, 0}, {2, 1}, {3, 2}, {4, 7}}};

patt = {x1_, x2_} /; x1 != 0 && x2 == 0;

ReplacePart[#, Thread[Position[#, patt] -> Cases[#, patt -> {x1, Na}, 2]]] &@a

(*{{{0, 0}, {1, 7}, {2, Na}, {3, 2}, {4, 7}}, 
 {{0, 0}, {1, Na}, {2, 1}, {3, 2}, {4, 7}}}*)
Post Deleted by E. Chan-López
Source Link
E. Chan-López
  • 31.2k
  • 3
  • 29
  • 50
Loading