'民'对于遇到历史数据的因素没有意义,R Studio

时间:2017-02-24 21:07:45

标签: r tapply

我有一个相当简单的动物遭遇历史数据集

Encounter history data - Shows each date data was collected and every animal that was seen during each encounter

我正在尝试运行一个函数,该函数将使用tapply代码显示每个动物的最早日期;

disco1<-tapply(dataSc$Date,dataSc$Final.ID,min) 

然而,它给了我以下错误;

  

Summary.factor中的错误(c(62L,72L,2L,63L,33L,59L,78L,39L),na.rm = FALSE):     'min'对因素没有意义

我不确定为什么它不起作用,因为我的主管对类似的数据集使用了相同的代码。任何帮助都会很棒:)

结构(清单(年份= c)(2009L,2009L,2011L,2011L,2011L,2011L ),日期=结构(c(48L,4L,60L,64L,16L,22L),.标签= c(&#34; 01/06 / 2012&#34;, &#34; 01/08/2011&#34;,&#34; 02/01 / 2014&#34;,&#34; 02/03/2009&#34;,&#34; 03/03/2009&#34 ;,&#34; 03/06 / 2015&#34;, &#34; 03/09/2011&#34;,&#34; 04/03 / 2015&#34;,&#34; 04/04 / 2015&#34;,&#34; 04/07 / 2012&#34 ;,&#34; 04/08/2012&#34;, &#34; 04/08/2015&#34;,&#34; 05/03/2009&#34;,&#34; 05/07/2011&#34;,&#34; 05/08/2015&#34 ;,&#34; 06/07/2011&#34;, &#34; 06/08/2015&#34;,&#34; 07/02 / 2013&#34;,&#34; 07/03 / 2013&#34;,&#34; 07/04 / 2015&#34 ;,&#34; 07/06 / 2015&#34;, &#34; 07/07/2011&#34;,&#34; 08/06 / 2009&#34;,&#34; 08/06 / 2015&#34;,&#34; 08/07 / 2012&#34 ;,&#34; 08/08/2012&#34;, &#34; 09/07 / 2015&#34;,&#34; 09/08/2011&#34;,&#34; 10/07 / 2011&#34;,&#34; 10/07 / 2013&#34 ;,&#34; 10/07 / 2015&#34;, &#34; 10/08/2015&#34;,&#34; 11/07 / 2012&#34;,&#34; 11/08/2011&#34;,&#34; 11/08 / 2012&#34 ;,&#34; 12/03/2014&#34;, &#34; 12/03 / 2015&#34;,&#34; 12/06 / 2015&#34;,&#34; 13/06 / 2013&#34;,&#34; 13/07 / 2012&#34 ;,&#34; 14/07 / 2015&#34;, &#34; 14/08/2012&#34;,&#34; 15/08/2011&#34;,&#34; 16/02/2009&#34;,&#34; 17/07 / 2011&#34 ;,&#34; 17/07 / 2012&#34;, &#34; 17/08/2011&#34;,&#34; 18/02/2009&#34;,&#34; 18/07/2011&#34;,&#34; 18/08/2011&#34 ;,&#34; 19/07/2011&#34;, &#34; 19/08/2011&#34;,&#34; 19/08/2015&#34;,&#34; 20/06 / 2011&#34;,&#34; 20/06 / 2015&#34 ;,&#34; 20/07/2012&#34;, &#34; 21/02/2009&#34;,&#34; 21/06 / 2011&#34;,&#34; 21 / 08/2012&#34;,&#34; 22/06 / 2011&#34 ;,&#34; 23/08/2012&#34;, &#34; 24/07 / 2011&#34;,&#34; 24/08/2011&#34;,&#34; 26 / 06/2011&#34;,&#34; 26/07 / 2011&#34 ;,&#34; 26/08/2011&#34;, &#34; 26/08/2012&#34;,&#34; 27/07 / 2015&#34;,&#34; 28/03 / 2015&#34;,&#34; 28/07 / 2011&#34 ;,&#34; 28/08/2012&#34;, &#34; 29/03/2015&#34;,&#34; 29/07 / 2011&#34;,&#34; 29/08/2011&#34;,&#34; 30/04 / 2015&#34 ;,&#34; 30/07/2012&#34;, &#34; 30/08/2011&#34;,&#34; 30/08/2012&#34;,&#34; 31/01 / 2013&#34;,&#34; 31/08/2011&#34 ;),class =&#34; factor&#34;),     Final.ID =结构(c(22L,12L,12L,28L,12L,17L),。Label = c(&#34; SWT1&#34;,     &#34; SWT11&#34;,&#34; SWT13&#34;,&#34; SWT14&#34;,&#34; SWT15&#34;,&#34; SWT16&#34;,&#34; SWT17& #34;,&#34; SWT19&#34;,     &#34; SWT2&#34;,&#34; SWT21&#34;,&#34; SWT22&#34;,&#34; SWT23&#34;,&#34; SWT24&#34;,&#34; SWT27& #34;,&#34; SWT28&#34;,     &#34; SWT29&#34;,&#34; SWT3&#34;,&#34; SWT30&#34;,&#34; SWT32&#34;,&#34; SWT37&#34;,&#34; SWT38& #34;,&#34; SWT45&#34;,     &#34; SWT46&#34;,&#34; SWT47&#34;,&#34; SWT48&#34;,&#34; SWT49&#34;,&#34; SWT5&#34;,&#34; SWT50& #34;,&#34; SWT51&#34;,     &#34; SWT52&#34;,&#34; SWT56&#34;,&#34; SWT58&#34;,&#34; SWT6&#34;,&#34; SWT61&#34;,&#34; SWT62& #34;,&#34; SWT65&#34;,     &#34; SWT7&#34;,&#34; SWT73&#34;,&#34; SWT8&#34;,&#34; SWT9&#34;,&#34; T-014&#34;,&#34 ; T001&#34;,&#34; T002&#34;,     &#34; T003&#34;,&#34; T004&#34;,&#34; T005&#34;,&#34; T005CC&#34;,&#34; T006&#34;,&#34; T006C& #34;,&#34; T009&#34;,     &#34; T010&#34;,&#34; T010C&#34;,&#34; T010CC&#34;,&#34; T011&#34;,&#34; T012&#34;,&#34; T013& #34;,&#34; T014&#34;,     &#34; T015&#34;,&#34; T015CC&#34;,&#34; T016&#34;,&#34; T018&#34;,&#34; T018C&#34;,&#34; T018CC& #34;,&#34; T018CCC&#34;,     &#34; T019&#34;,&#34; T019C&#34;,&#34; T019CC&#34;,&#34; T020&#34;,&#34; T021&#34;,&#34; T021C& #34;,&#34; T021CC&#34;,     &#34; T021CCC&#34;,&#34; T022&#34;,&#34; T022C&#34;,&#34; T022CC&#34;,&#34; T023&#34;,&#34; T024& #34;,&#34; T025&#34;,     &#34; T027&#34;,&#34; T029&#34;,&#34; T034&#34;,&#34; T041&#34;,&#34; T041C&#34;,&#34; T042& #34;,&#34; T043&#34;,     &#34; T044&#34;,&#34; T044C&#34;,&#34; T045&#34;,&#34; T045C&#34;,&#34; T046&#34;,&#34; T046C& #34;,&#34; T047&#34;,     &#34; T047C&#34;,&#34; T048&#34;,&#34; T049&#34;,&#34; T050&#34;,&#34; T051&#34;,&#34; T052& #34;,&#34; T053&#34;,     &#34; T053C&#34;,&#34; T054&#34;,&#34; T056&#34;,&#34; T056C&#34;,&#34; T057&#34;,&#34; T058& #34;,&#34; T059&#34;,     &#34; T060&#34;,&#34; T060C&#34;,&#34; T061&#34;,&#34; T063&#34;,&#34; T065&#34;,&#34; T065C& #34;,&#34; T068&#34;,     &#34; T070&#34;,&#34; T071&#34;,&#34; T072&#34;,&#34; T073&#34;,&#34; T074&#34;,&#34; T079& #34;,&#34; T082&#34;,&#34; T084&#34;,     &#34; T086&#34;,&#34; T087&#34;,&#34; T087C&#34;,&#34; T089&#34;,&#34; T091&#34;,&#34; T091C& #34;,&#34; T094&#34;,     &#34; T095&#34;,&#34; T096&#34;,&#34; T097&#34;,&#34; T097CC&#34;,&#34; T098&#34;,&#34; T099& #34;,&#34; T100&#34;,     &#34; T101&#34;,&#34; T101C&#34;,&#34; T102&#34;,&#34; T102C&#34;,&#34; T103&#34;,&#34; T104& #34;,&#34; T113&#34;,     &#34; T115&#34;,&#34; T117&#34;,&#34; T118&#34;,&#34; T118C&#34;,&#34; T123&#34;,&#34; T124& #34;,&#34; T124C&#34;,     &#34; T125&#34;,&#34; T126&#34;,&#34; T127&#34;,&#34; T128&#34;,&#34; T129&#34;,&#34; T130& #34;,&#34; T131&#34;,&#34; T132&#34;,     &#34; T133&#34;,&#34; T133C&#34;,&#34; T134&#34;,&#34; T135&#34;,&#34; T136&#34;,&#34; T137& #34;,&#34; T138&#34;,     &#34; T139&#34;,&#34; U011&#34;),class =&#34; factor&#34;),Data = structure(c(1L,     1L,1L,1L,1L,1L),。标签= c(&#34; Acoustic&#34;,&#34; Photo&#34;),class =&#34; factor&#34;))。。Name = c(&#34;年&#34;, &#34;日期&#34;,&#34; Final.ID&#34;,&#34;数据&#34;),row.names = c(NA,6L),class =&#34; data.frame& #34)

1 个答案:

答案 0 :(得分:0)

Min不起作用,因为Date列是一个因素,首先需要将其转换为Date,然后轻按:

df$Date <- as.Date(as.character(df$Date), tryFormats =  "%d/%m/%Y")
dates<-as.Date(tapply(df$Date, df$Final.ID, min), origin="1970-01-01")
dates[!is.na(dates)]
#>        SWT23         SWT3        SWT45        SWT50 
#> "2009-03-02" "2011-07-07" "2009-02-18" "2011-06-26"