2
votes

I have a data frame : see below and I would like to do a summaryby that will tell me the min and max of time (in second) for each CowID. But even though length(CowID)=length(Time), it doesn't work and I have the error :

Error in tapply(currVAR, rh.string.factor, function(x) { : arguments must have same length​

I wonder about the str of my data, there're a lot that is not useful like "Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 25852 obs. of 6 variables:" and what appears after $Time

> summary(LMD60)
     CowID            Date               DateHour                  
 2140   :  727   Min.   :2014-01-13   Min.   :2014-01-13 14:33:05  
 2019   :  366   1st Qu.:2014-01-20   1st Qu.:2014-01-20 15:33:46  
 2228   :  366   Median :2014-01-28   Median :2014-01-28 14:48:52  
 2234   :  366   Mean   :2014-01-27   Mean   :2014-01-28 04:26:46  
 2235   :  366   3rd Qu.:2014-02-04   3rd Qu.:2014-02-04 15:57:25  
 2047   :  365   Max.   :2014-02-12   Max.   :2014-02-12 16:10:39  
 (Other):23296                                                     
    Measure        Feeding           Time      
 Min.   :   8.0   hoko :15857   Min.   : 0.00  
 1st Qu.:  56.0   strap: 9995   1st Qu.:15.00  
 Median :  96.0                 Median :30.00  
 Mean   : 135.8                 Mean   :30.34  
 3rd Qu.: 168.0                 3rd Qu.:45.00  
 Max.   :1634.0                 Max.   :60.00  

> str(LMD60)
Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 25852 obs. of  6 variables:
 $ CowID   : Factor w/ 71 levels "1921","1923",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Date    : Date, format: "2014-01-27" "2014-01-27" ...
 $ DateHour: POSIXct, format: "2014-01-27 15:16:35" "2014-01-27 15:16:36" ...
 $ Measure : num  53 57 108 75 38 54 148 139 72 94 ...
 $ Feeding : Factor w/ 2 levels "hoko","strap": 1 1 1 1 1 1 1 1 1 1 ...
 $ Time    : num  0 1 1 2 2 3 3 4 4 5 ...
 - attr(*, "vars")=List of 2
  ..$ : symbol CowID
  ..$ : symbol Date
 - attr(*, "drop")= logi TRUE
 - attr(*, "indices")=List of 215
  ..$ : int  0 1 2 3 4 5 6 7 8 9 ...
  ..$ : int  121 122 123 124 125 126 127 128 129 130 ...
  ..$ : int  241 242 243 244 245 246 247 248 249 250 ...
  ..$ : int  362 363 364 365 366 367 368 369 370 371 ...
  ..$ : int  483 484 485 486 487 488 489 490 491 492 ...
  ..$ : int  604 605 606 607 608 609 610 611 612 613 ...
  ..$ : int  726 727 728 729 730 731 732 733 734 735 ...
  ..$ : int  847 848 849 850 851 852 853 854 855 856 ...
  ..$ : int  968 969 970 971 972 973 974 975 976 977 ...
  ..$ : int  1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 ...
  ..$ : int  1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 ...
  ..$ : int  1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 ...
  ..$ : int  1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 ...
  ..$ : int  1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 ...
  ..$ : int  1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 ...
  ..$ : int  1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 ...
  ..$ : int  1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 ...
  ..$ : int  2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 ...
  ..$ : int  2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 ...
  ..$ : int  2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 ...
  ..$ : int  2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 ...
  ..$ : int  2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 ...
  ..$ : int  2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 ...
  ..$ : int  2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 ...
  ..$ : int  2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 ...
  ..$ : int  3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 ...
  ..$ : int  3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 ...
  ..$ : int  3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 ...
  ..$ : int  3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 ...
  ..$ : int  3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 ...
  ..$ : int  3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 ...
  ..$ : int  3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 ...
  ..$ : int  3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 ...
  ..$ : int  3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 ...
  ..$ : int  4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 ...
  ..$ : int  4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 ...
  ..$ : int  4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 ...
  ..$ : int  4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 ...
  ..$ : int  4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 ...
  ..$ : int  4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 ...
  ..$ : int  4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 ...
  ..$ : int  4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 ...
  ..$ : int  5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 ...
  ..$ : int  5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 ...
  ..$ : int  5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 ...
  ..$ : int  5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 ...
  ..$ : int  5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 ...
  ..$ : int  5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 ...
  ..$ : int  5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 ...
  ..$ : int  5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 ...
  ..$ : int  6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 ...
  ..$ : int  6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 ...
  ..$ : int  6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 ...
  ..$ : int  6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 ...
  ..$ : int  6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 ...
  ..$ : int  6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 ...
  ..$ : int  6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 ...
  ..$ : int  6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 ...
  ..$ : int  6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 ...
  ..$ : int  7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 ...
  ..$ : int  7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 ...
  ..$ : int  7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 ...
  ..$ : int  7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 ...
  ..$ : int  7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 ...
  ..$ : int  7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 ...
  ..$ : int  7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 ...
  ..$ : int  7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 ...
  ..$ : int  8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 ...
  ..$ : int  8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 ...
  ..$ : int  8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 ...
  ..$ : int  8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 ...
  ..$ : int  8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 ...
  ..$ : int  8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 ...
  ..$ : int  8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 ...
  ..$ : int  8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 ...
  ..$ : int  9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 ...
  ..$ : int  9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 ...
  ..$ : int  9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 ...
  ..$ : int  9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 ...
  ..$ : int  9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 ...
  ..$ : int  9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 ...
  ..$ : int  9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 ...
  ..$ : int  9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 ...
  ..$ : int  9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 ...
  ..$ : int  10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 ...
  ..$ : int  10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 ...
  ..$ : int  10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 ...
  ..$ : int  10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 ...
  ..$ : int  10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 ...
  ..$ : int  10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 ...
  ..$ : int  10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 ...
  ..$ : int  10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 ...
  ..$ : int  11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 ...
  ..$ : int  11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 ...
  ..$ : int  11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 ...
  ..$ : int  11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 ...
  ..$ : int  11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 ...
  ..$ : int  11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 ...
  ..$ : int  11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 ...
  .. [list output truncated]
 - attr(*, "group_sizes")= int  121 120 121 121 121 122 121 121 121 121 ...
 - attr(*, "biggest_group_size")= int 122
 - attr(*, "labels")='data.frame':  215 obs. of  2 variables:
  ..$ CowID: Factor w/ 71 levels "1921","1923",..: 1 1 1 2 2 2 3 3 3 4 ...
  ..$ Date : Date, format: "2014-01-27" "2014-01-28" ...
  ..- attr(*, "vars")=List of 2
  .. ..$ : symbol CowID
  .. ..$ : symbol Date
  ..- attr(*, "drop")= logi TRUE

> summaryBy(LMD60$Time~LMD60$CowID, data=LMD60, FUN=list(min,max))

Error in tapply(currVAR, rh.string.factor, function(x) { : 
  arguments must have same length​
2
(a) where is summaryBy() defined (I'm assuming the doBy pkg)? (b) if it is doBy::summaryBy then you're using both $ and data which are not things to be mixed (c) you seem to have already used dplyr operations (it's the only way to get ‘grouped_df’, ‘tbl_df’, ‘tbl’) so why aren't you using it's own grouping and summarizing functions? (d) Time and Date seem to have been turned into more complex objects by some other process before this code snippet but ultimately Time is a numeric vector so it shld be fine.hrbrmstr
(a) Yes, doBy pkg. (b) So I should put only Time and CowID instead of LMD60$Time and LMD60$cowID ? But it doesn't work either... (c) Yes I've used dplyr, I don't know that package really well. I'm gonna check the summarizing functions of it.Marie
Okay thanks, I fix it by using the dplyr package. but I still have no idea why it didn't work. Do you think it will be a problem to apply statistic functions on the new data frame creating by the functions of the ddply package ? Or because of the str() I have ?Marie
We don't have your original data and have no idea how it got into this format so it's hard to discern how this ended up in this form. One thing to try outside of using dplyr functions would be to run LMD60 <- data.frame(LMD60) and see if your modified summaryBy() works that way (I don't use the doBy package at all given the flexibility and proper idioms of dplyr).hrbrmstr

2 Answers

6
votes

You have to change your data from tibble to dataframe (data= as.data.frame(data)). summaryBy works only on dataframes.

5
votes

I know I'm terribly late to this thread, but I just ran into the same problem as OP and hrbrmstr's recommendation of coercing it into a dataframe worked perfectly so I'm posting in case anyone else comes across this thread, try df <- as.data.frame(df) first.