Growth Rate Data

Hello, I have more of a data question here.

I have been measuring size of animals (mussels). Once per year (2016, 2017, 2019, 2021, and 2022). Length = l and Width = w. I am trying to measure growth rates for length and width.

Should I mutate a new column (see my code below) for each year? This seems clunky.

Should I convert these data from wide to long?

Also, there are a lot of missing data because the animals go missing. And I add new animals each year to the dataset. Is this the best data structure for this?

A bit of my code with what I am trying to do (plot initial length versus growth rate):

mussels$animal <- as.factor(mussels$animal)
#> Error in is.factor(x): object 'mussels' not found

mussels <- mussels %>%
  mutate(growth_2017 = (l_2017) -(l_2016))
#> Error in mussels %>% mutate(growth_2017 = (l_2017) - (l_2016)): could not find function "%>%"

plot1 <- mussels %>%
  ggplot(aes(x = l_2016, y = growth_2017)) +
  geom_point()
#> Error in mussels %>% ggplot(aes(x = l_2016, y = growth_2017)): could not find function "%>%"
plot1
#> Error in eval(expr, envir, enclos): object 'plot1' not found

Created on 2022-07-06 by the reprex package (v2.0.1)

Some of my data (hopefully Reprexed, correctly):

mussels2<- tibble::tribble(
             ~animal, ~l_2016, ~w_2016, ~l_2017, ~w_2017, ~l_2019, ~w_2019, ~l_2021, ~w_2021, ~l_2022, ~w_2022,
                  1L,    77.4,    23.6,    77.3,    23.5,   77.27,   39.87,      NA,      NA,      NA,      NA,
                  2L,      78,    27.5,      79,    27.4,   79.37,      45,      NA,      NA,      NA,      NA,
                  3L,    77.6,    21.7,    72.9,    23.6,   72.87,    37.5,      NA,      NA,      NA,      NA,
                  4L,    72.9,    23.2,    77.3,    21.5,   77.15,    38.5,      NA,      NA,      NA,      NA,
                  5L,    91.2,    30.4,    90.3,    30.9,   90.44,    51.4,      NA,      NA,      NA,      NA,
                  6L,   106.8,    31.9,   106.2,    32.2,      NA,      NA,      NA,      NA,      NA,      NA,
                  7L,      97,    30.7,    96.1,    29.7,   95.29,    53.5,      NA,      NA,      NA,      NA,
                  8L,    86.7,      28,    87.4,    28.7,      NA,      NA,      NA,      NA,      NA,      NA,
                  9L,    66.8,    20.9,    93.6,    25.7,      NA,      NA,      NA,      NA,      NA,      NA,
                 10L,    94.2,    25.6,    69.8,    21.3,   93.78,    47.4,      NA,      NA,      NA,      NA,
                 11L,    69.8,    21.1,    73.6,    22.7,   69.27,    37.1,      NA,      NA,      NA,      NA,
                 12L,    73.9,    22.7,    74.4,    24.5,   73.37,    35.9,      NA,      NA,      NA,      NA,
                 13L,    73.7,    23.1,    87.2,    27.8,   74.48,    38.2,      NA,      NA,      NA,      NA,
                 14L,    86.9,    27.7,    72.7,    19.6,   87.14,    49.1,      NA,      NA,      NA,      NA,
                 15L,      72,    19.5,      NA,      NA,   72.22,   38.64,      NA,      NA,      NA,      NA,
                 16L,      NA,      NA,    74.6,    39.3,   74.02,    40.6,   75.77,   40.64,      NA,      NA,
                 17L,      NA,      NA,    80.5,    45.7,   80.46,    45.5,      NA,      NA,      NA,      NA,
                 18L,      NA,      NA,    90.1,    48.6,      NA,      NA,      NA,      NA,      NA,      NA,
                 19L,      NA,      NA,    70.5,      37,   71.01,    36.5,      NA,      NA,      NA,      NA,
                 20L,      NA,      NA,    89.9,    48.7,   88.02,    48.8,   89.27,   48.85,      NA,      NA,
                 21L,      NA,      NA,    84.8,    42.5,   85.13,    43.1,      NA,      NA,      NA,      NA,
                 22L,      NA,      NA,    80.9,    45.3,      NA,      NA,      NA,      NA,      NA,      NA,
                 23L,      NA,      NA,    70.6,    38.1,   70.21,    37.6,      NA,      NA,      NA,      NA,
                 24L,      NA,      NA,    80.1,    42.7,   79.63,    41.9,      NA,      NA,      NA,      NA,
                 25L,      NA,      NA,    84.1,    49.8,   84.44,    49.7,      NA,      NA,      NA,      NA,
                 26L,      NA,      NA,    75.9,    38.3,    75.4,    74.4,      NA,      NA,      NA,      NA,
                 27L,      NA,      NA,    67.8,    35.5,   67.34,    34.8,      NA,      NA,      NA,      NA,
                 28L,      NA,      NA,    88.3,    44.9,    88.6,    48.1,      NA,      NA,      NA,      NA,
                 29L,      NA,      NA,    78.5,    42.7,   79.38,    42.8,   79.45,   43.22,      NA,      NA,
                 30L,      NA,      NA,      83,      46,   82.73,    46.1,      NA,      NA,      NA,      NA,
                 31L,      NA,      NA,      80,      46,      NA,      NA,   83.61,   44.86,      NA,      NA,
                 32L,      NA,      NA,      72,      40,      NA,      NA,      NA,      NA,      NA,      NA,
                 33L,      NA,      NA,      77,      44,      NA,      NA,      NA,      NA,      NA,      NA,
                 34L,      NA,      NA,      84,      42,   82.79,    42.5,      NA,      NA,      NA,      NA,
                 35L,      NA,      NA,      73,      37,      NA,      NA,      NA,      NA,      NA,      NA,
                 36L,      NA,      NA,      82,      41,   82.54,    41.4,      NA,      NA,      NA,      NA,
                 37L,      NA,      NA,      81,      45,      NA,      NA,      NA,      NA,      NA,      NA,
                 38L,      NA,      NA,      83,      47,   82.07,    46.7,      NA,      NA,      NA,      NA,
                 39L,      NA,      NA,      84,      45,   84.45,    44.9,      NA,      NA,      NA,      NA,
                 40L,      NA,      NA,      91,      49,   90.74,    47.7,   91.81,    48.3,      NA,      NA,
                 41L,      NA,      NA,      85,      44,   85.19,      45,   84.72,   45.28,      NA,      NA,
                 42L,      NA,      NA,      82,      46,      NA,      NA,      NA,      NA,      NA,      NA,
                 43L,      NA,      NA,      84,      42,      NA,      NA,      NA,      NA,      NA,      NA,
                 44L,      NA,      NA,      75,      41,   74.51,    41.5,      NA,      NA,      NA,      NA,
                 45L,      NA,      NA,      91,      49,   90.49,    48.8,   90.35,    46.8,      NA,      NA,
                 46L,      NA,      NA,      90,      50,      NA,      NA,      NA,      NA,      NA,      NA,
                 47L,      NA,      NA,      72,      35,   72.07,    36.7,      NA,      NA,      NA,      NA,
                 48L,      NA,      NA,      80,      43,   80.98,    43.6,   80.52,   43.75,      NA,      NA,
                 49L,      NA,      NA,      68,      36,   67.51,    34.8,      NA,      NA,      NA,      NA,
                 50L,      NA,      NA,      76,      41,   76.09,    41.8,      NA,      NA,      NA,      NA,
                 51L,      NA,      NA,      NA,      NA,   83.49,    43.4,      NA,      NA,      NA,      NA,
                 52L,      NA,      NA,      NA,      NA,   86.15,    47.1,      NA,      NA,      NA,      NA,
                 53L,      NA,      NA,      NA,      NA,   76.72,    41.3,      NA,      NA,      NA,      NA,
                 54L,      NA,      NA,      NA,      NA,   77.76,    45.6,      NA,      NA,      NA,      NA,
                 55L,      NA,      NA,      NA,      NA,   77.53,    43.5,   78.18,   43.02,      NA,      NA,
                 56L,      NA,      NA,      NA,      NA,   80.52,    42.9,      NA,      NA,      NA,      NA,
                 57L,      NA,      NA,      NA,      NA,   89.13,    49.6,      NA,      NA,      NA,      NA,
                 58L,      NA,      NA,      NA,      NA,   80.19,    45.6,      NA,      NA,      NA,      NA,
                 59L,      NA,      NA,      NA,      NA,   74.95,    40.1,   75.51,   40.68,      NA,      NA,
                 60L,      NA,      NA,      NA,      NA,   68.09,    35.9,    69.6,   35.48,      NA,      NA,
                 61L,      NA,      NA,      NA,      NA,   89.16,    49.8,      NA,      NA,      NA,      NA,
                 62L,      NA,      NA,      NA,      NA,      NA,      NA,   78.18,   43.02,      NA,      NA,
                 63L,      NA,      NA,      NA,      NA,      NA,      NA,   75.51,   40.68,      NA,      NA,
                 64L,      NA,      NA,      NA,      NA,      NA,      NA,    69.6,   35.48,      NA,      NA,
                 65L,      NA,      NA,      NA,      NA,      NA,      NA,   77.94,   41.08,      NA,      NA,
                 66L,      NA,      NA,      NA,      NA,      NA,      NA,   88.44,    50.5,      NA,      NA,
                 67L,      NA,      NA,      NA,      NA,      NA,      NA,   74.93,   40.77,      NA,      NA,
                 68L,      NA,      NA,      NA,      NA,      NA,      NA,   79.69,   43.17,      NA,      NA,
                 69L,      NA,      NA,      NA,      NA,      NA,      NA,   84.69,   84.67,      NA,      NA,
                 70L,      NA,      NA,      NA,      NA,      NA,      NA,   85.75,   48.82,      NA,      NA,
                 71L,      NA,      NA,      NA,      NA,      NA,      NA,   62.37,   33.72,      NA,      NA,
                 72L,      NA,      NA,      NA,      NA,      NA,      NA,   71.56,   38.59,      NA,      NA,
                 73L,      NA,      NA,      NA,      NA,      NA,      NA,   90.78,    47.1,      NA,      NA,
                 74L,      NA,      NA,      NA,      NA,      NA,      NA,   92.05,   49.47,      NA,      NA,
                 75L,      NA,      NA,      NA,      NA,      NA,      NA,   83.51,   49.01,      NA,      NA,
                 76L,      NA,      NA,      NA,      NA,      NA,      NA,   75.06,   40.91,      NA,      NA,
                 77L,      NA,      NA,      NA,      NA,      NA,      NA,   75.19,   38.61,      NA,      NA,
                 78L,      NA,      NA,      NA,      NA,      NA,      NA,   85.72,      43,      NA,      NA,
                 79L,      NA,      NA,      NA,      NA,      NA,      NA,   93.35,      52,      NA,      NA,
                 80L,      NA,      NA,      NA,      NA,      NA,      NA,   81.77,   47.66,      NA,      NA,
                 81L,      NA,      NA,      NA,      NA,      NA,      NA,   78.92,   42.42,      NA,      NA,
                 82L,      NA,      NA,      NA,      NA,      NA,      NA,   77.94,   41.08,      NA,      NA,
                 83L,      NA,      NA,      NA,      NA,      NA,      NA,   82.45,   43.79,      NA,      NA,
                 84L,      NA,      NA,      NA,      NA,      NA,      NA,   80.02,   46.88,      NA,      NA,
                 85L,      NA,      NA,      NA,      NA,      NA,      NA,    73.2,   72.72,      NA,      NA,
                 86L,      NA,      NA,      NA,      NA,      NA,      NA,   79.55,   43.99,      NA,      NA,
                 87L,      NA,      NA,      NA,      NA,      NA,      NA,    90.1,   48.31,      NA,      NA,
                 88L,      NA,      NA,      NA,      NA,      NA,      NA,   85.04,   50.29,      NA,      NA,
                 89L,      NA,      NA,      NA,      NA,      NA,      NA,   73.77,    41.6,      NA,      NA,
                 90L,      NA,      NA,      NA,      NA,      NA,      NA,   81.94,   46.83,      NA,      NA,
                 91L,      NA,      NA,      NA,      NA,      NA,      NA,      NA,      NA,   80.16,   47.69,
                 92L,      NA,      NA,      NA,      NA,      NA,      NA,      NA,      NA,    76.1,   44.16,
                 93L,      NA,      NA,      NA,      NA,      NA,      NA,      NA,      NA,   78.46,   43.42,
                 94L,      NA,      NA,      NA,      NA,      NA,      NA,      NA,      NA,   90.68,   50.26,
                 95L,      NA,      NA,      NA,      NA,      NA,      NA,      NA,      NA,   96.33,   51.32,
                 96L,      NA,      NA,      NA,      NA,      NA,      NA,      NA,      NA,   81.15,   47.81,
                 97L,      NA,      NA,      NA,      NA,      NA,      NA,      NA,      NA,   75.37,   41.31,
                 98L,      NA,      NA,      NA,      NA,      NA,      NA,      NA,      NA,   85.32,   46.78
             )

Created on 2022-07-06 by the reprex package (v2.0.1)

thank you!

Assuming that mussels2 is a copy or subset of mussels I do not see any reason

mussels$animal <- as.factor(mussels$animal)

is not working. It works for me. I would double-check spellings.

I think much of the rest of your problem is that you have not loaded a package. Try

library(tidyverse)

That should get rid of the %>% errors, at least. If necessary, install tidyverse.

install.packages("tidyverse")

Remember that the tidyverse works best when your data itself is tidy; that is, when every row is an observation and every column is a variable. So in your mussels2 dataset, you have observations embedded in column names, such as the type of measurement you are taking (length vs. width) and the year of the observation (2016, 2017, etc.). Therefore, you need to break those apart so that you can take maximal advantage of the tidyverse. To do that, I would pivot the data to long, then back to wide so that one row contains the length and width observations for a given animal in a given year. Then you can use dplyr::mutate with dplyr::across to get your growth rates in one go. Here's how I would do it:

library(dplyr)
library(tibble)
library(tidyr)

mussels2 %>% 
    pivot_longer(
        cols = 2:ncol(.)
    ) %>% 
    mutate(
        measurement = case_when(
            grepl('l', name) ~ 'length',
            grepl('w', name) ~ 'width'
        ),
        year = gsub('(l_)|(w_)', '', name) %>% 
            as.numeric
    ) %>% 
    pivot_wider(
        id_cols = c(animal, year),
        names_from = measurement,
        values_from = value
    ) %>% 
    arrange(animal, year) %>% 
    group_by(animal) %>% # To make sure we don't accidentally calc growth rates between animal ids
    mutate(
        across(
            length:width,
            function(x) { # I prefer this syntax for anonymous functions, but you can use a 
            # purrr style function if you want
                (x - lag(x)) / lag(x)
            },
            .names = '{.col}_growth_rate'
        )
    ) %>% 
    ungroup()
#> # A tibble: 490 x 6
#>    animal  year length width length_growth_rate width_growth_rate
#>     <int> <dbl>  <dbl> <dbl>              <dbl>             <dbl>
#>  1      1  2016   77.4  23.6          NA                 NA      
#>  2      1  2017   77.3  23.5          -0.00129           -0.00424
#>  3      1  2019   77.3  39.9          -0.000388           0.697  
#>  4      1  2021   NA    NA            NA                 NA      
#>  5      1  2022   NA    NA            NA                 NA      
#>  6      2  2016   78    27.5          NA                 NA      
#>  7      2  2017   79    27.4           0.0128            -0.00364
#>  8      2  2019   79.4  45             0.00468            0.642  
#>  9      2  2021   NA    NA            NA                 NA      
#> 10      2  2022   NA    NA            NA                 NA      
#> # ... with 480 more rows

Created on 2022-07-06 by the reprex package (v1.0.0)

Hopefully that helps. If you need help with the plots, I would recommend using this data.frame as a starting point.

@dvetsch75 This is exactly the advice I was looking for!

thank you!

@jrkrideau the "2" was a mistake--thanks for catching this.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.