<div>
It'd be easier if you just `melt` your `data.table` by `id = Respid`. Then you get a long format with which you can use fast grouping to count the number of 3s.
</div><div><br></div><div>require(reshape2)</div><div>require(data.table)</div><div><br></div><div>setkey(as.data.table(melt(DT, id="Respid")), Respid)[value == 3, .N, by=Respid][DT]</div><div><br></div>
<div><div><br></div><div>Arun</div><div><br></div></div>
<p style="color: #A0A0A8;">On Friday, August 30, 2013 at 9:04 AM, Serdar Akin wrote:</p>
<blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px;">
<span><div><div><div dir="ltr">Hi, <br>
<br>
Currently I'm trying to find a way to make row wise operation within data.table to find value that have<br>
a certain pattern, and to count those. <br>
<br>
For instance find the number of 3 for each Respid and count those for each row.<br>
set.seed(1)<br>
DT <- data.table( Respid = seq(1,100, 1), <br>
Q1 = rep(1:5, each = 20), <br>
Q2 = as.integer(runif(100, min = 1, max = 5) ),<br>
Q3 = sample(1:5, 100, replace = T)<br>
)<br>
<br>
DT1 <- DT[, lapply(.SD, function(x) length(grep(3, x))), by = 'Respid']<br>
<br>
A do get 1 for each column that has a 3 in it but no column that counts it. <br>
<br>
Regards Serdar</div>
</div><div><div>_______________________________________________</div><div>datatable-help mailing list</div><div><a href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a></div><div><a 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></div></div></div></span>
</blockquote>
<div>
<br>
</div>