Follower_Study

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

library(lme4)
Loading required package: Matrix
library(lmerTest)

Attaching package: 'lmerTest'
The following object is masked from 'package:lme4':

    lmer
The following object is masked from 'package:stats':

    step
library(emmeans)
library(car)
Loading required package: carData
library(tidyverse)
-- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
v dplyr     1.1.2     v readr     2.1.4
v forcats   1.0.0     v stringr   1.5.0
v ggplot2   3.4.3     v tibble    3.2.1
v lubridate 1.9.2     v tidyr     1.3.0
v purrr     1.0.1     
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x tidyr::expand() masks Matrix::expand()
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
x tidyr::pack()   masks Matrix::pack()
x dplyr::recode() masks car::recode()
x purrr::some()   masks car::some()
x tidyr::unpack() masks Matrix::unpack()
i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(corrplot)
corrplot 0.92 loaded
library(RColorBrewer)
library(ggplot2)
library(MASS)

Attaching package: 'MASS'

The following object is masked from 'package:dplyr':

    select
library(agricolae)
library(vegan)
Loading required package: permute
Loading required package: lattice
Registered S3 methods overwritten by 'vegan':
  method      from
  plot.rda    klaR
  predict.rda klaR
  print.rda   klaR
This is vegan 2.6-4
library(dplyr)
library(readr)
library(DT)
library(ggplot2)
library(quantreg)
Loading required package: SparseM

Attaching package: 'SparseM'

The following object is masked from 'package:base':

    backsolve

You can add options to executable code like this

rm(list = ls())
setwd("C:/Users/anune/OneDrive/Desktop/PIC_DataAnalysis_files")
data_PIC <- read.csv("PIC_65_FIRE.AN.1.csv")
data_PIC<- mutate(data_PIC,day_0 = dmy(START_DAY), 
       ENTRY_DATE = date(as.POSIXct(ENTRY_TIME, format= "%m/%d/%Y %H:%M")),
       DAYS_IN_FEED = as.numeric(ENTRY_DATE - day_0))
summary(data_PIC$DAYS_IN_FEED)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
 -364.0    14.0    31.0    31.2    48.0    68.0      25 
data_PIC <- group_by(data_PIC, ID)
head(data_PIC)
# A tibble: 6 x 19
# Groups:   ID [3]
        ID  LINE     SIRE    DAM LITTER PEN    FARM ENTRY_TIME EXIT_TIME STAY_IN
     <int> <int>    <int>  <int>  <int> <chr> <int> <chr>      <chr>       <int>
1 96251326    65 91032775 9.22e7 7.80e7 B0113   774 10/27/202~ 10/27/20~     649
2 96251327    65 91032775 9.22e7 7.80e7 B0113   774 10/27/202~ 10/27/20~    1948
3 96251327    65 91032775 9.22e7 7.80e7 B0113   774 10/27/202~ 10/27/20~     169
4 96284921    65 90218081 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     627
5 96284921    65 90218081 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     768
6 96284921    65 90218081 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     413
# i 9 more variables: FEED_INTK <int>, ENTRY_WT <int>, EXIT_WT <int>,
#   FEEDER_NO <int>, START_DAY <chr>, OFFTEST_DAY <chr>, day_0 <date>,
#   ENTRY_DATE <date>, DAYS_IN_FEED <dbl>
tail(data_PIC)
# A tibble: 6 x 19
# Groups:   ID [5]
        ID  LINE     SIRE    DAM LITTER PEN    FARM ENTRY_TIME EXIT_TIME STAY_IN
     <int> <int>    <int>  <int>  <int> <chr> <int> <chr>      <chr>       <int>
1 98350892    65 93693188 9.41e7 7.92e7 B0602   774 6/15/2023~ 6/15/202~     423
2 98350892    65 93693188 9.41e7 7.92e7 B0602   774 6/15/2023~ 6/15/202~    1237
3 98350934    65 93693188 9.41e7 7.92e7 B0602   774 6/19/2023~ 6/19/202~     183
4 98332707    65 93423760 9.42e7 7.92e7 B0602   774 6/19/2023~ 6/19/202~       6
5 98350859    65 93693188 9.41e7 7.92e7 B0602   774 6/19/2023~ 6/19/202~      14
6 98340247    65 93513866 9.42e7 7.92e7 B0602   774 6/19/2023~ 6/19/202~      29
# i 9 more variables: FEED_INTK <int>, ENTRY_WT <int>, EXIT_WT <int>,
#   FEEDER_NO <int>, START_DAY <chr>, OFFTEST_DAY <chr>, day_0 <date>,
#   ENTRY_DATE <date>, DAYS_IN_FEED <dbl>
data_PIC
# A tibble: 114,263 x 19
# Groups:   ID [548]
         ID  LINE    SIRE    DAM LITTER PEN    FARM ENTRY_TIME EXIT_TIME STAY_IN
      <int> <int>   <int>  <int>  <int> <chr> <int> <chr>      <chr>       <int>
 1 96251326    65  9.10e7 9.22e7 7.80e7 B0113   774 10/27/202~ 10/27/20~     649
 2 96251327    65  9.10e7 9.22e7 7.80e7 B0113   774 10/27/202~ 10/27/20~    1948
 3 96251327    65  9.10e7 9.22e7 7.80e7 B0113   774 10/27/202~ 10/27/20~     169
 4 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     627
 5 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     768
 6 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     413
 7 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~      18
 8 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     171
 9 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~      29
10 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     118
# i 114,253 more rows
# i 9 more variables: FEED_INTK <int>, ENTRY_WT <int>, EXIT_WT <int>,
#   FEEDER_NO <int>, START_DAY <chr>, OFFTEST_DAY <chr>, day_0 <date>,
#   ENTRY_DATE <date>, DAYS_IN_FEED <dbl>

The echo: false option disables the printing of code (only output is displayed).

#| warning: true
#| echo: true
class(data_PIC)
[1] "grouped_df" "tbl_df"     "tbl"        "data.frame"
names(data_PIC)
 [1] "ID"           "LINE"         "SIRE"         "DAM"          "LITTER"      
 [6] "PEN"          "FARM"         "ENTRY_TIME"   "EXIT_TIME"    "STAY_IN"     
[11] "FEED_INTK"    "ENTRY_WT"     "EXIT_WT"      "FEEDER_NO"    "START_DAY"   
[16] "OFFTEST_DAY"  "day_0"        "ENTRY_DATE"   "DAYS_IN_FEED"
str(data_PIC)
gropd_df [114,263 x 19] (S3: grouped_df/tbl_df/tbl/data.frame)
 $ ID          : int [1:114263] 96251326 96251327 96251327 96284921 96284921 96284921 96284921 96284921 96284921 96284921 ...
 $ LINE        : int [1:114263] 65 65 65 65 65 65 65 65 65 65 ...
 $ SIRE        : int [1:114263] 91032775 91032775 91032775 90218081 90218081 90218081 90218081 90218081 90218081 90218081 ...
 $ DAM         : int [1:114263] 92185339 92185339 92185339 90208620 90208620 90208620 90208620 90208620 90208620 90208620 ...
 $ LITTER      : int [1:114263] 78043216 78043216 78043216 78064883 78064883 78064883 78064883 78064883 78064883 78064883 ...
 $ PEN         : chr [1:114263] "B0113" "B0113" "B0113" "B0113" ...
 $ FARM        : int [1:114263] 774 774 774 774 774 774 774 774 774 774 ...
 $ ENTRY_TIME  : chr [1:114263] "10/27/2022 12:51" "10/27/2022 12:00" "10/27/2022 13:03" "10/27/2022 7:27" ...
 $ EXIT_TIME   : chr [1:114263] "10/27/2022 13:02" "10/27/2022 12:32" "10/27/2022 13:06" "10/27/2022 7:37" ...
 $ STAY_IN     : int [1:114263] 649 1948 169 627 768 413 18 171 29 118 ...
 $ FEED_INTK   : int [1:114263] 265 514 57 85 196 88 3 33 4 14 ...
 $ ENTRY_WT    : int [1:114263] 1056 1086 791 473 613 691 591 588 555 551 ...
 $ EXIT_WT     : int [1:114263] 791 572 734 388 417 603 588 555 551 537 ...
 $ FEEDER_NO   : int [1:114263] 5 5 5 5 5 5 5 5 5 5 ...
 $ START_DAY   : chr [1:114263] "27-Oct-22" "27-Oct-22" "27-Oct-22" "27-Oct-22" ...
 $ OFFTEST_DAY : chr [1:114263] "2-Jan-23" "2-Jan-23" "2-Jan-23" "2-Jan-23" ...
 $ day_0       : Date[1:114263], format: "2022-10-27" "2022-10-27" ...
 $ ENTRY_DATE  : Date[1:114263], format: "2022-10-27" "2022-10-27" ...
 $ DAYS_IN_FEED: num [1:114263] 0 0 0 0 0 0 0 0 0 0 ...
 - attr(*, "groups")= tibble [548 x 2] (S3: tbl_df/tbl/data.frame)
  ..$ ID   : int [1:548] 96251326 96251327 96251328 96251346 96263569 96263570 96263592 96263593 96263594 96263848 ...
  ..$ .rows: list<int> [1:548] 
  .. ..$ : int [1:193] 1 80 81 624 625 626 965 966 967 1714 ...
  .. ..$ : int [1:136] 2 3 86 87 632 633 968 1721 1819 1820 ...
  .. ..$ : int [1:221] 92 93 94 251 252 983 984 985 986 1729 ...
  .. ..$ : int [1:194] 102 103 987 988 989 1735 1745 1746 1747 1824 ...
  .. ..$ : int [1:200] 107 108 109 110 111 263 264 265 1000 1001 ...
  .. ..$ : int [1:208] 112 113 114 115 618 619 620 621 1078 1079 ...
  .. ..$ : int [1:315] 129 256 257 258 259 260 261 381 382 1327 ...
  .. ..$ : int [1:253] 266 267 268 387 388 389 390 547 548 549 ...
  .. ..$ : int [1:177] 394 395 396 563 564 595 596 597 622 623 ...
  .. ..$ : int [1:244] 262 408 409 410 411 604 605 606 607 615 ...
  .. ..$ : int [1:250] 416 417 418 608 609 610 627 628 629 630 ...
  .. ..$ : int [1:203] 4 5 6 7 8 9 10 11 12 13 ...
  .. ..$ : int [1:216] 15 16 17 18 19 687 688 689 690 691 ...
  .. ..$ : int [1:212] 20 21 22 23 24 25 26 27 28 29 ...
  .. ..$ : int [1:159] 30 31 32 33 68 712 713 952 953 1073 ...
  .. ..$ : int [1:181] 73 74 75 76 724 725 726 727 957 958 ...
  .. ..$ : int [1:218] 82 83 739 740 741 969 970 971 972 973 ...
  .. ..$ : int [1:224] 34 88 89 90 757 758 759 974 975 976 ...
  .. ..$ : int [1:221] 95 96 97 98 770 771 772 773 990 991 ...
  .. ..$ : int [1:184] 35 36 37 38 104 105 1002 1003 1004 1053 ...
  .. ..$ : int [1:390] 39 40 121 122 123 124 125 550 551 552 ...
  .. ..$ : int [1:192] 41 42 43 383 384 567 568 1335 1336 1337 ...
  .. ..$ : int [1:225] 44 391 598 599 600 649 650 651 1762 1893 ...
  .. ..$ : int [1:153] 397 398 601 602 603 667 668 1076 1077 1895 ...
  .. ..$ : int [1:296] 45 46 47 48 49 50 419 420 611 612 ...
  .. ..$ : int [1:224] 51 704 705 706 707 708 954 955 956 1694 ...
  .. ..$ : int [1:231] 52 53 59 60 61 62 63 714 715 716 ...
  .. ..$ : int [1:179] 64 65 66 67 728 729 730 1325 1326 1709 ...
  .. ..$ : int [1:225] 69 70 71 72 742 743 744 938 939 940 ...
  .. ..$ : int [1:259] 77 78 79 749 750 751 752 959 960 961 ...
  .. ..$ : int [1:208] 84 85 760 761 962 963 964 1731 1732 1733 ...
  .. ..$ : int [1:238] 54 55 56 91 996 997 998 999 1045 1046 ...
  .. ..$ : int [1:189] 99 100 101 948 949 950 951 1055 1056 1756 ...
  .. ..$ : int [1:204] 106 1005 1006 1060 1763 1764 2186 2510 2511 2512 ...
  .. ..$ : int [1:217] 116 117 118 553 554 555 1317 1318 1319 2191 ...
  .. ..$ : int [1:125] 119 120 565 566 932 2526 2527 2708 3644 4674 ...
  .. ..$ : int [1:178] 126 127 128 569 634 635 1133 1134 1135 2715 ...
  .. ..$ : int [1:184] 385 386 636 637 1152 1153 1154 2726 2727 2728 ...
  .. ..$ : int [1:105] 1086 1087 2204 2205 2206 2207 4504 4505 4506 4864 ...
  .. ..$ : int [1:200] 392 393 669 670 1801 1802 2740 2741 3670 3671 ...
  .. ..$ : int [1:219] 399 400 401 402 403 404 405 406 407 678 ...
  .. ..$ : int [1:239] 412 413 414 415 696 697 698 699 1911 1912 ...
  .. ..$ : int [1:229] 421 422 423 424 425 426 709 710 717 718 ...
  .. ..$ : int [1:183] 427 428 429 719 720 2169 2467 2468 2469 2660 ...
  .. ..$ : int [1:249] 731 732 733 734 2181 2182 2482 2483 2484 2485 ...
  .. ..$ : int [1:205] 745 746 1035 1036 1037 2187 2188 2492 2493 2494 ...
  .. ..$ : int [1:196] 753 754 1136 1137 1138 1528 1529 1530 2196 2197 ...
  .. ..$ : int [1:270] 762 763 764 765 1155 1156 1157 1158 1543 1544 ...
  .. ..$ : int [1:163] 1049 1050 1051 1564 1565 1796 1797 2718 2719 2720 ...
  .. ..$ : int [1:183] 1057 1058 1059 1566 1567 1568 1569 1570 1571 1803 ...
  .. ..$ : int [1:220] 1061 1062 1063 1587 1588 1813 1814 2742 2743 2744 ...
  .. ..$ : int [1:224] 542 543 544 545 546 1605 1606 1607 1608 1609 ...
  .. ..$ : int [1:144] 556 557 558 559 1621 1622 1623 1624 1921 2139 ...
  .. ..$ : int [1:173] 560 561 562 1625 1626 2150 2151 2470 2471 2472 ...
  .. ..$ : int [1:251] 638 639 640 641 1638 1639 1640 1641 1642 1643 ...
  .. ..$ : int [1:169] 642 643 1969 1970 1971 2170 2171 2239 2240 2241 ...
  .. ..$ : int [1:233] 747 748 1811 1812 2005 2006 2007 2328 2329 2599 ...
  .. ..$ : int [1:277] 368 685 686 1052 1919 1920 2014 2015 2016 2017 ...
  .. ..$ : int [1:191] 721 722 723 1975 1976 1977 2183 2184 2242 2243 ...
  .. ..$ : int [1:241] 755 756 1139 1140 1141 1142 1978 1979 1980 2189 ...
  .. ..$ : int [1:293] 652 653 654 655 656 1159 1160 1161 1987 1988 ...
  .. ..$ : int [1:203] 657 658 659 1798 1799 1800 1994 1995 1996 2302 ...
  .. ..$ : int [1:254] 700 701 1922 2019 2020 2021 2031 2361 2362 2363 ...
  .. ..$ : int [1:192] 735 736 737 738 2032 2033 2034 2124 2125 2126 ...
  .. ..$ : int [1:209] 766 767 768 769 866 867 1038 1039 1040 1041 ...
  .. ..$ : int [1:224] 671 672 673 868 869 870 871 1042 1043 1044 ...
  .. ..$ : int [1:217] 154 269 270 271 272 273 274 914 915 1148 ...
  .. ..$ : int [1:345] 167 168 169 170 171 300 301 302 1193 1194 ...
  .. ..$ : int [1:207] 181 182 183 329 330 331 1212 1213 1214 1215 ...
  .. ..$ : int [1:156] 711 891 892 893 1131 1132 1548 1549 2713 2714 ...
  .. ..$ : int [1:186] 136 137 138 910 911 912 913 1143 1144 1145 ...
  .. ..$ : int [1:192] 225 226 347 348 1237 1238 1239 1644 1645 2201 ...
  .. ..$ : int [1:176] 468 469 470 1007 1008 1009 1010 1011 1309 1310 ...
  .. ..$ : int [1:208] 354 355 356 357 358 359 360 361 362 432 ...
  .. ..$ : int [1:199] 369 438 439 1253 1254 1255 1256 1257 2245 2246 ...
  .. ..$ : int [1:265] 449 450 1258 1259 1260 1261 1262 1263 2079 2080 ...
  .. ..$ : int [1:175] 201 202 203 336 337 338 339 1217 1218 1219 ...
  .. ..$ : int [1:204] 451 452 453 454 1306 1307 1308 2092 2093 2094 ...
  .. ..$ : int [1:189] 471 472 473 474 1956 1957 2082 2310 2601 2602 ...
  .. ..$ : int [1:208] 489 490 1958 1959 1960 2083 2084 2330 2331 2332 ...
  .. ..$ : int [1:152] 498 499 1961 1962 1963 2095 2096 2097 2333 2629 ...
  .. ..$ : int [1:197] 275 276 277 278 279 280 281 282 283 284 ...
  .. ..$ : int [1:168] 293 511 1344 1345 1972 1973 2366 2367 2368 2396 ...
  .. ..$ : int [1:239] 303 304 305 306 307 308 529 530 531 1974 ...
  .. ..$ : int [1:186] 130 131 132 317 318 319 320 321 322 323 ...
  .. ..$ : int [1:174] 143 144 332 1020 1346 1984 1985 1986 2697 2851 ...
  .. ..$ : int [1:166] 145 340 341 1021 1022 1023 1024 1347 1997 1998 ...
  .. ..$ : int [1:157] 163 164 349 1520 2000 2001 2002 2091 2877 3399 ...
  .. ..$ : int [1:121] 180 350 363 1348 1521 1522 2003 2004 2878 2879 ...
  .. ..$ : int [1:250] 192 193 370 371 1349 1350 1351 1531 1532 1533 ...
  .. ..$ : int [1:153] 194 195 196 1352 1353 1553 1554 2012 2013 2916 ...
  .. ..$ : int [1:220] 211 212 213 214 1354 1355 1576 1577 1578 2022 ...
  .. ..$ : int [1:192] 227 1356 1357 1358 1596 1597 1598 1599 2027 2028 ...
  .. ..$ : int [1:269] 434 435 1359 1360 1361 1600 1601 1602 1603 1604 ...
  .. ..$ : int [1:237] 440 441 442 1362 1363 1364 1365 1614 1615 2045 ...
  .. ..$ : int [1:318] 455 456 457 1384 1385 1630 1631 1632 1633 1634 ...
  .. ..$ : int [1:241] 286 287 288 289 458 459 460 461 849 850 ...
  .. ..$ : int [1:380] 294 295 296 309 310 475 476 477 478 854 ...
  .. ..$ : int [1:149] 311 312 479 480 863 864 865 1415 2236 2237 ...
  .. .. [list output truncated]
  .. ..@ ptype: int(0) 
  ..- attr(*, ".drop")= logi TRUE
data_PIC$PEN <- as.factor(data_PIC$PEN)

data_PIC$Social_Group <- paste(data_PIC$PEN, data_PIC$START_DAY, data_PIC$OFFTEST_DAY, sep = "_")

head(data_PIC$Social_Group)
[1] "B0113_27-Oct-22_2-Jan-23" "B0113_27-Oct-22_2-Jan-23"
[3] "B0113_27-Oct-22_2-Jan-23" "B0113_27-Oct-22_2-Jan-23"
[5] "B0113_27-Oct-22_2-Jan-23" "B0113_27-Oct-22_2-Jan-23"
data_PIC <- group_by(data_PIC, Social_Group)

data_PIC
# A tibble: 114,263 x 20
# Groups:   Social_Group [36]
         ID  LINE    SIRE    DAM LITTER PEN    FARM ENTRY_TIME EXIT_TIME STAY_IN
      <int> <int>   <int>  <int>  <int> <fct> <int> <chr>      <chr>       <int>
 1 96251326    65  9.10e7 9.22e7 7.80e7 B0113   774 10/27/202~ 10/27/20~     649
 2 96251327    65  9.10e7 9.22e7 7.80e7 B0113   774 10/27/202~ 10/27/20~    1948
 3 96251327    65  9.10e7 9.22e7 7.80e7 B0113   774 10/27/202~ 10/27/20~     169
 4 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     627
 5 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     768
 6 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     413
 7 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~      18
 8 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     171
 9 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~      29
10 96284921    65  9.02e7 9.02e7 7.81e7 B0113   774 10/27/202~ 10/27/20~     118
# i 114,253 more rows
# i 10 more variables: FEED_INTK <int>, ENTRY_WT <int>, EXIT_WT <int>,
#   FEEDER_NO <int>, START_DAY <chr>, OFFTEST_DAY <chr>, day_0 <date>,
#   ENTRY_DATE <date>, DAYS_IN_FEED <dbl>, Social_Group <chr>
data_PIC.arrange <- arrange(data_PIC, Social_Group, ENTRY_TIME, by_group = TRUE)%>%
  mutate(line= row_number())


data_PIC.arrange
# A tibble: 114,263 x 21
# Groups:   Social_Group [36]
         ID  LINE    SIRE    DAM LITTER PEN    FARM ENTRY_TIME EXIT_TIME STAY_IN
      <int> <int>   <int>  <int>  <int> <fct> <int> <chr>      <chr>       <int>
 1 97900500    65  9.20e7 9.15e7 7.86e7 B0111   774 3/15/2023~ 3/15/202~      62
 2 97887847    65  8.83e7 9.34e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~      13
 3 97916804    65  9.20e7 9.37e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~     144
 4 97900594    65  8.82e7 9.22e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~       8
 5 97887845    65  8.83e7 9.34e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~     713
 6 97900594    65  8.82e7 9.22e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~    2124
 7 97900500    65  9.20e7 9.15e7 7.86e7 B0111   774 3/15/2023~ 3/15/202~       5
 8 97916804    65  9.20e7 9.37e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~     132
 9 97900489    65  8.83e7 9.18e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~    1033
10 97900600    65  8.82e7 9.22e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~     533
# i 114,253 more rows
# i 11 more variables: FEED_INTK <int>, ENTRY_WT <int>, EXIT_WT <int>,
#   FEEDER_NO <int>, START_DAY <chr>, OFFTEST_DAY <chr>, day_0 <date>,
#   ENTRY_DATE <date>, DAYS_IN_FEED <dbl>, Social_Group <chr>, line <int>
data_PIC.arrange %>%
  dplyr::select(ID, ENTRY_TIME, Social_Group)
# A tibble: 114,263 x 3
# Groups:   Social_Group [36]
         ID ENTRY_TIME      Social_Group             
      <int> <chr>           <chr>                    
 1 97900500 3/15/2023 10:01 B0111_16-Mar-23_15-May-23
 2 97887847 3/15/2023 10:05 B0111_16-Mar-23_15-May-23
 3 97916804 3/15/2023 10:10 B0111_16-Mar-23_15-May-23
 4 97900594 3/15/2023 10:14 B0111_16-Mar-23_15-May-23
 5 97887845 3/15/2023 10:16 B0111_16-Mar-23_15-May-23
 6 97900594 3/15/2023 10:29 B0111_16-Mar-23_15-May-23
 7 97900500 3/15/2023 11:06 B0111_16-Mar-23_15-May-23
 8 97916804 3/15/2023 11:10 B0111_16-Mar-23_15-May-23
 9 97900489 3/15/2023 11:15 B0111_16-Mar-23_15-May-23
10 97900600 3/15/2023 11:33 B0111_16-Mar-23_15-May-23
# i 114,253 more rows
#| warning: true
#| echo: true
data_PIC <- data_PIC %>%
  arrange(Social_Group, ENTRY_TIME) %>%
  group_by(Social_Group) %>%
  mutate(Follower_ID = lag(ID),
         Follower_Time = ifelse(is.numeric(lag(ENTRY_TIME)), log(lag(ENTRY_TIME)), NA),
         Follower_Social_Group = lag(Social_Group))

data_PIC
# A tibble: 114,263 x 23
# Groups:   Social_Group [36]
         ID  LINE    SIRE    DAM LITTER PEN    FARM ENTRY_TIME EXIT_TIME STAY_IN
      <int> <int>   <int>  <int>  <int> <fct> <int> <chr>      <chr>       <int>
 1 97900500    65  9.20e7 9.15e7 7.86e7 B0111   774 3/15/2023~ 3/15/202~      62
 2 97887847    65  8.83e7 9.34e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~      13
 3 97916804    65  9.20e7 9.37e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~     144
 4 97900594    65  8.82e7 9.22e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~       8
 5 97887845    65  8.83e7 9.34e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~     713
 6 97900594    65  8.82e7 9.22e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~    2124
 7 97900500    65  9.20e7 9.15e7 7.86e7 B0111   774 3/15/2023~ 3/15/202~       5
 8 97916804    65  9.20e7 9.37e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~     132
 9 97900489    65  8.83e7 9.18e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~    1033
10 97900600    65  8.82e7 9.22e7 7.90e7 B0111   774 3/15/2023~ 3/15/202~     533
# i 114,253 more rows
# i 13 more variables: FEED_INTK <int>, ENTRY_WT <int>, EXIT_WT <int>,
#   FEEDER_NO <int>, START_DAY <chr>, OFFTEST_DAY <chr>, day_0 <date>,
#   ENTRY_DATE <date>, DAYS_IN_FEED <dbl>, Social_Group <chr>,
#   Follower_ID <int>, Follower_Time <lgl>, Follower_Social_Group <chr>