[datatable-help] New column with conditions for first and last observation by id group

Frank S. f_j_rod at hotmail.com
Fri Jul 25 14:29:59 CEST 2014


Hi all, I'm very novice in data.table management, and I have the following doubt about this data: > DT <- data.table(obs=1:7, id=c(1,1,1,4,4,4,4), time=c(3,4,7,5,8,10,15))> DT   obs id time1:   1  1    32:   2  1    43:   3  1    74:   4  4    55:   5  4    86:   6  4   107:   7  4   15 In general, I know that I can select respectively the first and the last observation within "id" group with:   First observation: DT[!duplicated(id)] Last observation: DT[!duplicated(id, fromLast=T)]  But, how can I add a new column, called "value", which contains all zeros except:1) The first observation within each "id" group, which is equal to 22) The last observation within each "id" group, which is equal to 1 ?    obs id time value1:   1  1    3     22:   2  1    4     03:   3  1    7     14:   4  4    5     25:   5  4    8     06:   6  4   10     07:   7  4   15     1 I've tried with conditionslas, ifelse, etc, but I get an error message. Please, can you help me? Thanks in advance!!            		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20140725/cca9c657/attachment.html>


More information about the datatable-help mailing list