I want to get the average date from the line, where there are two dates in each row. Finally I found a way, post below Done However, the approach I use is very difficult. Is there a better way?
my.data = read.table (text = "OBS MONTH1 days 1 YEAR1 MONTH2 DAY2 YEAR2 STATE 1 3 6 2012 3 10 2012 1 2 3 10 2012 3 20 2012 1 3 3 16 2012 3 30 2012 1 4 3 20 2012 4 8 2012 1 5 3 20 2012 4 9 2012 1 6 3 20 2012 4 10 2012 1 7 3 20 2012 4 11 2012 1 8 4 4 2012 4 5 2012 194 6 2012 4 6 2012 1 10 4 6 2012 4 7 2012 1 ", header = TRUE, stringsfactors = FALSE my.data my.data $ MY.DATE1 & lt; - do.call (paste, list (my.data $ MONTH1, my.data $ DAY1, my.data $ YEAR1) my.data $ MY.DATE2 & lt; - do.call (paste, list (my.data $ MONTH2, my.data $ DAY2, my.data $ YEAR2) My.data $ MY.DATE1 & lt; - as.Date (my.data $ MY.DATE1, f Ormat = c ("% m% d% Y")) my.data $ MY.DATE2 & lt; - as.Date (my.data $ MY.DATE2, format = c ("% m% d% Y")) My.data desired.Parthic = readable (text = "OBS month 1 day 1 year 1 month 2 days 2 Year State My.Data 1 MY.DATE2 i.e. 1 3 6 2012 3 10 2012 1 2012-03-06 2012-03-10 2012 -03-08 2 3 10 2012 3 20 2012 1 2012-03-10 2012-03- 20 2012-03-15 3 3 16 2012 3 30 2012 1 2012-03-16 2012-03-30 2012-03-23 4 3 20 2012 4 2012 2012 2012-20-20 2012-04-08 2012-03 -29 5 3 20 2012 4 9 2012 1 2012-03-20 2012-04-09 2012-03-30 6 3 20 2012 4 2012 2012-03-20 2012-04-10 2012-03-30 7 3 20 2012 4 11 2012 1 2012-03-20 2012-04-11 2012-03-31 8 4 4 2012 4 2012 2012 2012-04-04 2012-04-05 2012-04-04 9 42 2012 4 6 2012 1 2012-04-06 2012-04-06 2012-04-06 10 4 6 20 12 4 7 2012 1 2012-04-06 2012-04-07 2012-04-06 ", header = TRUE, stringsfactor = FALSE )
this Rika is the way to work for me:
my.data $ mean.date & lt; - (my.data $ MY.DATE1 + ((my.data $ MY.DATE2 - my.data $ MY.DATE1) / 2)) My.data These approaches Do not work:
my.data $ mean.date & lt; - Mean (my.data $ MY.DATE1, My.data $ MY.DATE2) my.data $ mean.date & lt; - Mean (my.data $ MY.DATE1, my.data $ MY.DATE2, trim = 0) my.data $ mean.date & lt; - Mean (my.data $ MY.DATE1, my.data $ MY.DATE2, trim = 1) my.data $ mean.date & lt; - Mean (my.data $ MY.DATE1, my.data $ MY.DATE2, trim = 0.5) my.data $ mean.data & lt; - (My data, 1, function (x) {(x [9] + x [10]) / 2})
I think the ops.date
should use the command, but no examples are found.
Thanks for any suggestions.
Using the good advice of @ jaysunice3401, I came up with this. If you want to keep the original data, you add the remove = FALSE
to unite
library with the library (dplyr) in two rows (Tidyr) my.data% & gt;% (which also combines 1, matches ("1"), Sep = "-"%>% 1 (whichever is 2, match ("2"), seep = "-")% 1 is included in Mutate_each (funs (as.Date (., "% M-% d-% Y")) ("whatever")%>% rowwise% & gt; ;% Mutate (mean.date = mean.Date) (C (Joe 1, J2)) # OBS Whatever 2 states means mean.date # 1 1 2012-03-06 2012-03-10 1 2012 -03-08 # 2 2012-03-10 2012-03-20 1 2012-03-15 # 3 3 2012-03-16 2012-03-30 1 2012-03-23 # 4 4 2012-03 -20 2012-04-08 1 2012-03-29 # 5 5 2012-03- 20 2012-04-09 1 2012- 03-30 # 6 6 2012-03-20 2012-04-10 1 2012-03-30 # 7 7 2012-03-20 2012-04-11 1 2012-03-31 # 8 8 2012-04-04 2012 -04-05 1 2012-04-04 # 9 9/2012-04-06 2012-04-06 1 2012-04-06 # 10 10 2012-04-06 2012-04- 07 1 2012-04-06
Comments
Post a Comment