<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 13/03/14 14:48, Manabu Sakamoto
      wrote:<br>
    </div>
    <blockquote
cite="mid:CAErHMT2G8aWUC1vLkyDMaa9YBJUCtjkCPAGRgS7mTfKwfcDKvA@mail.gmail.com"
      type="cite">
      <div dir="ltr">Dear list
        <div><br>
        </div>
        <div>I'm trying to access values within a data.table column by
          matching to elements in a vector and replacing with
          corresponding elements in a second vector. But I want to loop
          through specific column names also stored as a character
          vector.</div>
        <div>So something like:</div>
        <div><br>
        </div>
        <div>DT<span></span> <-
          data.table(A=seq(1:10),B=seq(1:10),C=seq(1:10))</div>
        <div><br>
        </div>
        cnm <- c("A", "B", "C")<br>
        <div>before <- c(4, 5, 6)</div>
        <div>after <- c(3, 7, 8)</div>
        <div>
          <br>
        </div>
        nm <- cnm[i]
        <div>bfr <- before[i]</div>
        <div>afr <- after[i]<br>
          <div><br>
          </div>
          <div>DT[nm==bfr, nm:=afr]</div>
        </div>
        <div><br>
        </div>
        <div>I'm sure this is completely wrong because it didn't work.</div>
        <div>So does anyone know how to correctly do this data.table
          solution?</div>
      </div>
    </blockquote>
    <br>
    DT[get(nm)==bfr, (nm):=afr]<br>
    <br>
    or<br>
    <br>
    set(DT, i=DT[[nm]]==bfr, j=nm, value=afr)<br>
    <br>
    I prefer the first way but if you're looping through many columns
    (say 1,000+) then using set() should be faster, see ?set.<br>
    <br>
    HTH, Matt<br>
    <br>
    <blockquote
cite="mid:CAErHMT2G8aWUC1vLkyDMaa9YBJUCtjkCPAGRgS7mTfKwfcDKvA@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div><br>
        </div>
        <div>Many thanks,</div>
        <div>Manabu</div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
datatable-help mailing list
<a class="moz-txt-link-abbreviated" href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a>
<a class="moz-txt-link-freetext" href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a></pre>
    </blockquote>
    <br>
  </body>
</html>