I tried using two ways to add a column in a data.table, it returned different resuts. But I don't understand why, could you please give me a hint? Way 1:
avg_tvd <- dev_survey4[Grp==0 | Grp==1, .(avgTVD = mean(TVDmASL, na.rm=TRUE)),
by = .(Grp,WELL,APA_Pair_ID)]
Here are the results:
Way 2:
avg_tvd <- dev_survey4[Grp==0 | Grp==1, avgTVD := mean(TVDmASL, na.rm=TRUE),
by = .(Grp,WELL,APA_Pair_ID)]
Here are results:
The results of way 1 are what I want. But why way 2 has different results? There are two differences between them:
- Columns of ways 2 are more than way 1;
- Row of way 2 has Grps besides 0 and 1.


=for aggregating/summarising, result has same number of rows as number of unique values inby:=for adding a column, result has the same number of rows as the originalFor example:
Created on 2018-06-16 by the reprex package (v0.2.0).