Creating new column based on earliest date value in other column in R -


I have a data frame that looks like this:

  topic = c (1 , 1, 1,1,2,2,2,2) OBS = C (1.3,2.4,3.3,4.2,1.2,2.7,3.6,4.9) REFDAT = like. DET (C ('2013-09-25' , '2013-09-25', '2013-09-26', '2013-09-26', '2013-09-27', '2013- 09-27', '2013-09-30', ' 2013 - 09-30 ')) df1 = data.frame (subject, OBS, REFDATE)   

I want to add a new column (REFDATE2) which contains only the most for each First topic REFDATE , produce it:

  topic = c (1,1,1,1,2,2,2,2) OBS = C (1.3,2.4,3.3 , 4.2, 1.22.7,3.64.9) REFDAT = Edge. Date (c ('2013-09-25', '2013-09-25', '2013-09-26', '2013-09-26', '2013-09-27', '2013- 09-27 ',' 2013-09-30 ',' 2013-09-30 ')) REFDT2 = ASD (c (' 2013-09-25 ',' 2013-09-25 ',' 2013-09-25 ' , '2013-09-25', '2013-09-27', '2013- 09-27', '2013-09-27', '2013-09 -27') df2 = data.frame (subject,  

Synergy,

ykl

/ p>

/ P>

or using the data.table package Which is easy and fast)

  Library (dataable) df1 & lt; - as.data.frame (data.table (df1) [, REFDATE2: = theme by = (REFDATE, by)) df1 # Subject OBS REFDATE REFDATE2 # 1 1 1.3 2013-09-25 2013-09-25 # 2 1 2.4 2013-09-25 2013-09-25 # 3 1 3.3 2013-09-26 2013-09-25 # 4 1 4.2 2013- 09-26 2013-09-25 # 5 2 1.2 2013-09-27 2013 -09-27 # 6 2 2.7 2013-09-27 2013-09-27 # 7 2 3.6 2013-09-30, 2013-09-27 # 8 2 4.9 2013-09-30 2013-09-27   

edit

per OPs request, if you want to add time, instead of as the date like like .pOSIXct < / Code>, Something like this:

  topic = c (1,1,1,1,2,2,2,2,2) OBS = C (1.3,2.4,3.3,4.2 , 1.22.7.3.6.4.9) REFDAT = ASPOSICX (C ('2013-09-25 08:30:00', '2013- 09-25 08: 3 0:00 ',' 2013-09-26 08:30:00 ',' 2013-09-26 08:30:00 ',' 2013-09-27 08:30:00, ' '2013-09-27 08:30:00', '2013-09-30 08:30:00', '2013-09-30 08:30:00')) df1 = data.frame (subject, OBS, REFDATE) Library (Data Table) df1 & lt; - as.data.frame (DataW1) [, REFDATE2: = Minutes (by Rfidadet), = Subject) df1 # Subject OBS REFDATE2 # 1 1 1.3 2013-09-25 08: 30:00 2013-09-25 08 : 30: 00 # 2 1 2.4 2013-09-25 08:30:00 2013-09-25 08:30:00 # 3 1 3.3 2013-09-26 08:30: 00 2013-09-25 08:30 : 00 # 4 1 4.2 2013-09-26 08:30:00 2013-09-25 08:30:00 # 5 2 1.2 2013-09-27 08:30:00 2013 -0 9-27 08:30: 00 # 6 2 2.7 2013-09-27 08:30:00 2013-09-27 08:30:00 # 7 2 3.6 2013-09-30 08:30:00 2013-09 -27 08:30:00 # 8 2 4.9 2013-09-30 08:30:00 2013-09-27 08:30:00    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -