I am new to R and having trouble trying to execute my plan.
I am trying to add another column to my data frame with the values coming from the rows below. However, each row must come from a different number of rows below.
I have 6-7 age cohorts and temperature values for multiple years. Cohort 1 will have that year's temperature value, cohort 2 will have the year prior, cohort 3 will have the temperature value 2 years prior, etc.
This is the data in a wide format. Wide format data. I will need the final output in a long format, such as long format
This is my current workings
long_density %>%
mutate( Summer_prior_2 = Summer_max_prior) %>%
mutate(Summer_prior_2 = c(Summer_max_prior[-1]))
select(Year, Cohort, Density, Summer_max_prior, Summer_prior_2, Winter_min_post, Summer_max_post) %>%
View
long_density %>%
mutate( Summer_prior_2 = Summer_max_prior) %>%
mutate(Summer_prior_2 = ifelse("Cohort" == "Cohort_1", Summer_prior_2,
if_else("Cohort" == "Cohort_2",Summer_prior_2[-7], NA))) %>%
View
I was thinking a "ifelse" code where cohort_1 = Cohort_1, Cohort_2= Cohort_2 - 7, Cohort_3= Cohort_3 - 14, Cohort_4= Cohort_4 - 21, etc.. As there are 7 cohorts in in each year the value I want is increasing by 7 for each cohort.
EDIT
Sorry, my original question wasn't too clear.
I know how to pivot_longer. It's mutating the temperature data depending on the cohort that I am having difficulties with.
Here is an example data frame:
df <- data.frame (Year<- as.numeric (c("2021","2020","2019","2018","2017")),
Cohort_1 <- as.numeric (c("12", "13", "12", "14", "20")),
Cohort_2 <- as.numeric (c("23", "22", "23", "26", "29")),
Cohort_3 <- as.numeric (c("32", "32", "40", "35", "34")),
Cohort_4 <- as.numeric (c("44", "43", "40", "49", "46")),
Cohort_5 <- as.numeric (c("56", "49", "41", "50", "55")),
Cohort_6 <- as.numeric (c("66", "61", "62", "69", "68")),
Cohort_7 <- as.numeric (c("77", "90", "82", "84", "79")),
Summer_max_prior <- as.numeric (c("2","3","4","6","4")),
Winter_min_post <- as.numeric (c("1","2","0","3","1")))
> df
Year Cohort_1 Cohort_2 Cohort_3 Cohort_4 Cohort_5 Cohort_6 Cohort_7 Summer_max Winter_min
1 2021 12 23 32 44 56 66 77 2 1
2 2020 13 22 32 43 49 61 90 3 2
3 2019 12 23 40 40 41 62 82 4 0
4 2018 14 26 35 49 50 69 84 6 3
5 2017 20 29 34 46 55 68 79 4 1
This is the pivot_longer code.
long_density <- pivot_longer(df,cols=c("Cohort_1","Cohort_2","Cohort_3","Cohort_4","Cohort_5","Cohort_6","Cohort_7"), names_to= "Cohort",
values_to= "Density")
> long_density %>% select(Year, Cohort, Density, Summer_max, Winter_min)
Year Cohort Density Summer_max Winter_min
1 2021 Cohort_1 12 2 1
2 2021 Cohort_2 23 2 1
3 2021 Cohort_3 32 2 1
4 2021 Cohort_4 44 2 1
5 2021 Cohort_5 56 2 1
6 2021 Cohort_6 66 2 1
7 2021 Cohort_7 77 2 1
8 2020 Cohort_1 13 3 2
9 2020 Cohort_2 22 3 2
10 2020 Cohort_3 32 3 2
I need both Summer_max and Winter_min data for cohort 2 (2-year-olds) to be from the year before, cohort 3 (3-year-olds) to be 3 years before, and so on. I need the corresponding temperatures for the year that the cohort of fish spawned.
At the moment all values for each cohort within each year are the same. This is the code I have written so far but r does not like my ifelse code.
long_density %>%
mutate(Summer_prior_2 = Summer_max_prior) %>%
mutate(Summer_prior_2 = ifelse(long_density$Cohort == "Cohort_1", Summer_max_prior,
if_else(long_density$Cohort == "Cohort_2",Summer_max_prior[-7],
if_else(long_density$Cohort == "Cohort_3",Summer_max_prior[-14],
if_else(long_density$Cohort == "Cohort_4",Summer_max_prior[-21],
if_else(long_density$Cohort == "Cohort_5",Summer_max_prior[-28],
if_else(long_density$Cohort == "Cohort_6",Summer_max_prior[-35],
if_else(long_density$Cohort == "Cohort_7",Summer_max_prior[-42], NA))))))))
Any help would be greatly appreciated!