From half-hour to hourly data - r

My data set contains observations taken every 30 minutes. Basically, I would like to have such as 01:00:00 as the new time; 02/01/2019 as an exampe of date and the sum of the measurement values between 00:00:00-00:30:00 and 00:30:00-01:00:00 As an output
time variable value
01:00:00 02/01/2019 234.3 (example)
How can I aggregate my data to 1 hour?
Sample data: Selected only the first 300
structure(list(time = structure(c(1800, 3600, 5400, 7200, 9000,
10800, 12600, 14400, 16200, 18000, 19800, 21600, 23400, 25200,
27000, 28800, 30600, 32400, 34200, 36000, 37800, 39600, 41400,
43200, 45000, 46800, 48600, 50400, 52200, 54000, 55800, 57600,
59400, 61200, 63000, 64800, 66600, 68400, 70200, 72000, 73800,
75600, 77400, 79200, 81000, 82800, 84600, 86400, 1800, 3600,
5400, 7200, 9000, 10800, 12600, 14400, 16200, 18000, 19800, 21600,
23400, 25200, 27000, 28800, 30600, 32400, 34200, 36000, 37800,
39600, 41400, 43200, 45000, 46800, 48600, 50400, 52200, 54000,
55800, 57600, 59400, 61200, 63000, 64800, 66600, 68400, 70200,
72000, 73800, 75600, 77400, 79200, 81000, 82800, 84600, 86400,
1800, 3600, 5400, 7200, 9000, 10800, 12600, 14400, 16200, 18000,
19800, 21600, 23400, 25200, 27000, 28800, 30600, 32400, 34200,
36000, 37800, 39600, 41400, 43200, 45000, 46800, 48600, 50400,
52200, 54000, 55800, 57600, 59400, 61200, 63000, 64800, 66600,
68400, 70200, 72000, 73800, 75600, 77400, 79200, 81000, 82800,
84600, 86400, 1800, 3600, 5400, 7200, 9000, 10800, 12600, 14400,
16200, 18000, 19800, 21600, 23400, 25200, 27000, 28800, 30600,
32400, 34200, 36000, 37800, 39600, 41400, 43200, 45000, 46800,
48600, 50400, 52200, 54000, 55800, 57600, 59400, 61200, 63000,
64800, 66600, 68400, 70200, 72000, 73800, 75600, 77400, 79200,
81000, 82800, 84600, 86400, 1800, 3600, 5400, 7200, 9000, 10800,
12600, 14400, 16200, 18000, 19800, 21600, 23400, 25200, 27000,
28800, 30600, 32400, 34200, 36000, 37800, 39600, 41400, 43200,
45000, 46800, 48600, 50400, 52200, 54000, 55800, 57600, 59400,
61200, 63000, 64800, 66600, 68400, 70200, 72000, 73800, 75600,
77400, 79200, 81000, 82800, 84600, 86400, 1800, 3600, 5400, 7200,
9000, 10800, 12600, 14400, 16200, 18000, 19800, 21600, 23400,
25200, 27000, 28800, 30600, 32400, 34200, 36000, 37800, 39600,
41400, 43200, 45000, 46800, 48600, 50400, 52200, 54000, 55800,
57600, 59400, 61200, 63000, 64800, 66600, 68400, 70200, 72000,
73800, 75600, 77400, 79200, 81000, 82800, 84600, 86400, 1800,
3600, 5400, 7200, 9000, 10800, 12600, 14400, 16200, 18000, 19800,
21600), class = c("hms", "difftime"), units = "secs"), variable = c("02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "08/01/2019", "08/01/2019", "08/01/2019",
"08/01/2019", "08/01/2019", "08/01/2019", "08/01/2019", "08/01/2019",
"08/01/2019", "08/01/2019", "08/01/2019", "08/01/2019"), value = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)), row.names = c(NA, -300L), class = c("tbl_df",
"tbl", "data.frame"))

I've got an answer, that uses {lubridate}:
library(lubridate)
a_df %>%
mutate(datetime = lubridate::dmy_hms(str_c(variable, time))) %>%
mutate(duration_elapsed = lubridate::interval(datetime[1], datetime),
duration_elapsed = as.duration(duration_elapsed)) %>%
mutate(hourly_group = duration_elapsed %/% dhours()) %>%
glimpse() %>%
group_by(hourly_group) %>%
summarise(
value = sum(value),
time = last(time),
variable = last(variable),
datetime = last(datetime)
) %>%
# select(-hourly_group, -time, -variable) %>%
select(-hourly_group) %>%
print(width = Inf, n = 50)
Note that a_df is the data-frame you've provided.
This should work, and it should be robust if you have missing numbers,
etc.

Related

Piecewise regression with multiple covariables with segmented package

I want to make some piecewise regression with the segmented package on R, but I'm a beginner with this package and I don't find how to do what I want.
Here, I have a plot with 6 curves that shows the performance (axis y) depending of 3 parameters (quantity on x-axis; quality, distinguished by colors ; temperature, distinguished by a gradient of color).
(color is a combination of temperature and quality to distinguish them on my ggplot, but every variable exists independently and in combination).
I want to make a piecewise regression for each curve to compare breaking points of these curves, but with my code, I only get one .
Here is the code for that :
fitJ4 = lm(growth_rate ~ quantity, data = donnees_tot_g_J4)
segmented.fitJ4 = segmented(fitJ4, seg.Z = ~quantity)
fit=numeric(length(donnees_tot_g_J4$quantity))*NA
fit[complete.cases(rowSums(cbind(donnees_tot_g_J4$growth_rate,donnees_tot_g_J4$quantity)))] = broken.line(segmented.fitJ4)$fit
ggplot(donnees_tot_g_J4, aes(x=quantity, y=growth_rate, col=color))+
geom_point(size=3,aes(col=color))+
geom_line (aes (x = quantity, y = fit, color= color), alpha=0.2)+
scale_x_continuous(breaks=c(0.1,0.3,0.6,0.9,1.5), limits=c(0.1,1.5))+
scale_y_continuous(limits=c(0,0.8))+
scale_colour_manual(values=c("20S" = "aquamarine1","25S" = "aquamarine3","28S" = "aquamarine4","20Y" = "darkgoldenrod1","25Y" = "darkgoldenrod3", "28Y" = "darkgoldenrod4"))+
scale_fill_manual(values=c("20S" = "aquamarine1","25S" = "aquamarine3","28S" = "aquamarine4","20Y" = "darkgoldenrod1", "25Y" = "darkgoldenrod3","28Y" = "darkgoldenrod4"))+
theme_minimal()+
xlab("quantity") + ylab("perf")+
theme_grey(base_size = 22)
Here, I understood that the problem was that I only write "quantity" for the seg.z, but when I tried to write that :
segmented.fitJ4 = segmented(fitJ4, seg.Z = ~quantity+quality+temperature)
It doesn't work because "Length or names of Z and psi do not match". So, I know that I need to define the psi, but I don't know what to put in for the quality and temperature...
I hope it is clear. I give you some of my data to try. Thank you!
structure(list(name = c("J4_S03AC", "J4_S03BC", "J4_S03CC", "J4_S03DC",
"J4_S06BC", "J4_S06CC", "J4_S06DC", "J4_S09AC", "J4_S09BC", "J4_S09CC",
"J4_S09DC", "J4_Y03AC", "J4_Y03BC", "J4_Y03CC", "J4_Y06AC", "J4_Y06BC",
"J4_Y06CC", "J4_Y06DC", "J4_Y09AC", "J4_Y09BC", "J4_Y09CC", "J4_Y09DC",
"J4_S01AM", "J4_S01BM", "J4_S01CM", "J4_S01DM", "J4_S15AM", "J4_S15BM",
"J4_S15CM", "J4_Y01AM", "J4_Y01BM", "J4_Y01CM", "J4_Y01DM", "J4_Y15AM",
"J4_Y15BM", "J4_Y15CM", "J4_S01AC", "J4_S01CC", "J4_S01EC", "J4_S01FC",
"J4_S03BC", "J4_S03CC", "J4_S03DC", "J4_S03EC", "J4_S03FC", "J4_S06AC",
"J4_S06DC", "J4_S06EC", "J4_S06FC", "J4_S06KC", "J4_S06MC", "J4_S06NC",
"J4_S09AC", "J4_S09BC", "J4_S09CC", "J4_S09DC", "J4_Y01AC", "J4_Y01BC",
"J4_Y01CC", "J4_Y06AC", "J4_Y06BC", "J4_Y06CC", "J4_Y06DC", "J4_Y09AC",
"J4_Y09BC", "J4_Y09CC", "J4_Y09DC", "J4_S03AM", "J4_S03BM", "J4_S03CM",
"J4_S15AM", "J4_S15BM", "J4_S15CM", "J4_S15DM", "J4_Y03AM", "J4_Y03BM",
"J4_Y03CM", "J4_Y03DM", "J4_Y15AM", "J4_Y15BM", "J4_Y15CM", "J4_Y15DM",
"J4_Y15AC", "J4_Y15BC", "J4_Y15CC", "J4_Y15DC", "J4_Y15EC", "J4_Y15FC",
"J4_Y15GC", "J4_Y15HC", "J4_Y15IC", "J4_Y15JC", "J4_Y15KC", "J4_Y15LC",
"J4_Y15MC", "J4_Y15NC", "J4_Y15OC", "J4_Y15PC", "J4_Y15QC", "J4_Y15RC",
"J4_Y15SC", "J4_Y15TC", "J4_Y15UC", "J4_Y15VC", "J4_Y15WC", "J4_S01AAC",
"J4_S01AC", "J4_S01BC", "J4_S01CC", "J4_S06AC", "J4_S06BC", "J4_S06CC",
"J4_S06DC", "J4_S09AC", "J4_S09BC", "J4_S09CC", "J4_S09DC", "J4_Y01AC",
"J4_Y01CC", "J4_Y06AC", "J4_Y06BC", "J4_Y06CC", "J4_Y06DC", "J4_Y09AC",
"J4_Y09BC", "J4_Y09CC", "J4_Y09DC", "J4_S03AM", "J4_S03BM", "J4_S03CM",
"J4_S15AM", "J4_S15BM", "J4_S15CM", "J4_Y03AM", "J4_Y03BM", "J4_Y03CM",
"J4_Y03DM", "J4_Y15AM", "J4_Y15BM", "J4_Y15CM", "J4_Y15DM"),
day = c("J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4"), quality = c("S", "S", "S", "S", "S", "S", "S",
"S", "S", "S", "S", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"Y", "Y", "Y", "S", "S", "S", "S", "S", "S", "S", "Y", "Y",
"Y", "Y", "Y", "Y", "Y", "S", "S", "S", "S", "S", "S", "S",
"S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S",
"S", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"S", "S", "S", "S", "S", "S", "S", "Y", "Y", "Y", "Y", "Y",
"Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"Y", "Y", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S",
"S", "S", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"S", "S", "S", "S", "S", "S", "Y", "Y", "Y", "Y", "Y", "Y",
"Y", "Y"), quantity = c(0.3, 0.3, 0.3, 0.3, 0.6, 0.6, 0.6,
0.9, 0.9, 0.9, 0.9, 0.3, 0.3, 0.3, 0.6, 0.6, 0.6, 0.6, 0.9,
0.9, 0.9, 0.9, 0.1, 0.1, 0.1, 0.1, 1.5, 1.5, 1.5, 0.1, 0.1,
0.1, 0.1, 1.5, 1.5, 1.5, 0.1, 0.1, 0.1, 0.1, 0.3, 0.3, 0.3,
0.3, 0.3, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.9, 0.9, 0.9,
0.9, 0.1, 0.1, 0.1, 0.6, 0.6, 0.6, 0.6, 0.9, 0.9, 0.9, 0.9,
0.3, 0.3, 0.3, 1.5, 1.5, 1.5, 1.5, 0.3, 0.3, 0.3, 0.3, 1.5,
1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
1.5, 1.5, 0.1, 0.1, 0.1, 0.1, 0.6, 0.6, 0.6, 0.6, 0.9, 0.9,
0.9, 0.9, 0.1, 0.1, 0.6, 0.6, 0.6, 0.6, 0.9, 0.9, 0.9, 0.9,
0.3, 0.3, 0.3, 1.5, 1.5, 1.5, 0.3, 0.3, 0.3, 0.3, 1.5, 1.5,
1.5, 1.5), qual_quant = c("S03", "S03", "S03", "S03", "S06",
"S06", "S06", "S09", "S09", "S09", "S09", "Y03", "Y03", "Y03",
"Y06", "Y06", "Y06", "Y06", "Y09", "Y09", "Y09", "Y09", "S01",
"S01", "S01", "S01", "S15", "S15", "S15", "Y01", "Y01", "Y01",
"Y01", "Y15", "Y15", "Y15", "S01", "S01", "S01", "S01", "S03",
"S03", "S03", "S03", "S03", "S06", "S06", "S06", "S06", "S06",
"S06", "S06", "S09", "S09", "S09", "S09", "Y01", "Y01", "Y01",
"Y06", "Y06", "Y06", "Y06", "Y09", "Y09", "Y09", "Y09", "S03",
"S03", "S03", "S15", "S15", "S15", "S15", "Y03", "Y03", "Y03",
"Y03", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15",
"Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15",
"Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15",
"Y15", "S01", "S01", "S01", "S01", "S06", "S06", "S06", "S06",
"S09", "S09", "S09", "S09", "Y01", "Y01", "Y06", "Y06", "Y06",
"Y06", "Y09", "Y09", "Y09", "Y09", "S03", "S03", "S03", "S15",
"S15", "S15", "Y03", "Y03", "Y03", "Y03", "Y15", "Y15", "Y15",
"Y15"), temperature = c(20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25), time = c(105,
105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105,
105, 105, 105, 105, 105, 105, 105, 105, 105, 112, 112, 112,
112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 101,
101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101,
101, 101, 101, 102, 102, 102, 102, 102, 102, 102, 102, 102,
102, 102, 102, 102, 102, 102, 107.5, 107.5, 107.5, 107.5,
107.5, 107.5, 107.5, 107.5, 107.5, 107.5, 107.5, 107.5, 107.5,
107.5, 107.5, 108, 108, 108, 108, 108, 108, 108, 108, 108,
108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108,
108, 108, 109, 104, 104, 104, 104, 104, 104, 104, 104, 104,
104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104,
112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112,
112, 112), size = c(1681.616, 1769.388, 1640.182, 1749.236,
1772.734, 1751.192, 1538.992, 1771.498, 1712.612, 1648.414,
1964.914, 1693.468, 1990.782, 1660.296, 2125.122, 2181.51,
1936.8, 2047.176, 1886.724, 2262.896, 2267.56, 2078.214,
1523.922, 1452.24, 1621.674, 1479.096, 1625.708, 1826.186,
1675.434, 1591.866, 1574.092, 1583.592, 1595.3, 2199.116,
2334.592, 2098.526, 1344.366, 1291.962, 1304.811, 1128.264,
1286.151, 1358.421, 1396.671, 1353.076, 1505.565, 1297.17,
0, 1243.029, 1323.85, 1368.364, 1506.396, 0, 1663.735, 1632.28,
2115.303, 1921.46, 1407.825, 1248.956, 1402.668, 1910.167,
2106.313, 2016.868, 2043.982, 2188.033, 1983.409, 2326.42,
2082.324, 1506.581, 1501.196, 1370.99, 1870.489, 1941.425,
1942.186, 1827.395, 1713.149, 1996.395, 2011.623, 1798.349,
2171.226, 2252.35, 2071.279, 2485.113, 2215.084, 2380.252,
2337.003, 2384.119, 2371.255, 2437.183, 2355.166, 2384.359,
2256.135, 2413.416, 2251.848, 2326.666, 2463.171, 2419.457,
2418.257, 2409.51, 2399.095, 2415.043, 2474.581, 2529.032,
2401.006, 2392.16, 2462.465, 1336.588, 1184.353, 1239.748,
1460.383, 1635.684, 1724.768, 1754.185, 1737.566, 1753.176,
1959.044, 1701.156, 1770.752, 1336.1, 1372.907, 2175.332,
1997.874, 1896.37, 2269.925, 2213.498, 2192.229, 2041.227,
2474.221, 1628.119, 1662.795, 1578.399, 1911.944, 2045.866,
2190.013, 1690.419, 1672.256, 2133.311, 1772.034, 2423.857,
2492.047, 2703.446, 2459.673), weight = c(34, 53, 36, 34,
42, 46, 48, 63, 54, 51, 71, 65, 63, 61, 91, 93, 96, 84, 93,
137, 140, 125, 18, 26, 22, 21, 47, 52, 44, 20, 19, 19, 24,
155, 142, 136, 11, 16, 10, 10, 12, 12, 13, 16, 14, 12, 11,
12, 10, 10, 15, 25, 46, 35, 66, 46, 17, 15, 11, 71, 68, 68,
58, 115, 96, 114, 82, 20, 16, 15, 49, 73, 63, 60, 35, 39,
44, 27, 99, 94, 86, 168, 126, 134, 124, 130, 139, 183, 136,
143, 79, 129, 163, 148, 164, 186, 144, 155, 125, 133, 143,
161, 135, 142, 155, 11, 13, 12, 17, 39, 42, 42, 28, 37, 37,
20, 39, 14, 15, 70, 61, 53, 73, 107, 74, 77, 93, 28, 23,
24, 50, 42, 68, 31, 23, 38, 31, 119, 139, 162, 112), fecundity = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 6, 0, 0, 9, 6, 7, 7, 6, 7, 10, 7, 8, 5, 6, 10, 7, 8, 8,
7, 6, 8, 8, 9, 8, 8, 2, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 7, 8, 12, 6), metabolic_rate = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.346,
0.357, 0.3585, 0.512, 1.528, 1.927, 1.431, 0.225, 0.268,
0.471, 0.477, 3.334, 3.103, 3.384, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0.382, 0.1435, 0.2405, 1.436, 2.014, 3.464, 2.742,
1.624, 1.909, 2.014, 1.392, 5.058, 4.439, 4.242, 7.193, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), time_maturity = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 104, 0, 0, 104, 104,
104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104,
104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 139.52,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110.03, 110.03, 110.03, 110.03
), weight_maturity = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 142.231, 0, 0, 142.231, 142.231,
142.231, 142.231, 142.231, 142.231, 142.231, 142.231, 142.231,
142.231, 142.231, 142.231, 142.231, 142.231, 142.231, 142.231,
142.231, 142.231, 142.231, 142.231, 142.231, 142.231, 142.231,
142.231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, NA,
NA, NA), aire = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, NA,
NA, NA), jour = c(4.375035, 4.375035, 4.375035, 4.375035,
4.375035, 4.375035, 4.375035, 4.375035, 4.375035, 4.375035,
4.375035, 4.375035, 4.375035, 4.375035, 4.375035, 4.375035,
4.375035, 4.375035, 4.375035, 4.375035, 4.375035, 4.375035,
4.666704, 4.666704, 4.666704, 4.666704, 4.666704, 4.666704,
4.666704, 4.666704, 4.666704, 4.666704, 4.666704, 4.666704,
4.666704, 4.666704, 4.208367, 4.208367, 4.208367, 4.208367,
4.208367, 4.208367, 4.208367, 4.208367, 4.208367, 4.208367,
4.208367, 4.208367, 4.208367, 4.208367, 4.208367, 4.208367,
4.250034, 4.250034, 4.250034, 4.250034, 4.250034, 4.250034,
4.250034, 4.250034, 4.250034, 4.250034, 4.250034, 4.250034,
4.250034, 4.250034, 4.250034, 4.4792025, 4.4792025, 4.4792025,
4.4792025, 4.4792025, 4.4792025, 4.4792025, 4.4792025, 4.4792025,
4.4792025, 4.4792025, 4.4792025, 4.4792025, 4.4792025, 4.4792025,
4.500036, 4.500036, 4.500036, 4.500036, 4.500036, 4.500036,
4.500036, 4.500036, 4.500036, 4.500036, 4.500036, 4.500036,
4.500036, 4.500036, 4.500036, 4.500036, 4.500036, 4.500036,
4.500036, 4.500036, 4.500036, 4.500036, 4.500036, 4.541703,
4.333368, 4.333368, 4.333368, 4.333368, 4.333368, 4.333368,
4.333368, 4.333368, 4.333368, 4.333368, 4.333368, 4.333368,
4.333368, 4.333368, 4.333368, 4.333368, 4.333368, 4.333368,
4.333368, 4.333368, 4.333368, 4.666704, 4.666704, 4.666704,
4.666704, 4.666704, 4.666704, 4.666704, 4.666704, 4.666704,
4.666704, 4.666704, 4.666704, 4.666704, 4.666704), growth_rate = c(0.303800071834841,
0.405269291837872, 0.316864747623708, 0.303800071834841,
0.352098906853808, 0.37289228981091, 0.382620123855391, 0.444775904433979,
0.409541745203879, 0.396477069415012, 0.472100291701688,
0.451919285802152, 0.444775904433979, 0.437402052060633,
0.52882661034269, 0.533795710392497, 0.541052497667245, 0.510531280665662,
0.533795710392497, 0.622339329512085, 0.627290489167077,
0.601386996896272, 0.148530350448613, 0.22732788723803, 0.191530869757777,
0.181562374726831, 0.354194957377592, 0.375858237686643,
0.34006122020639, 0.17110742318728, 0.160116090892034, 0.160116090892034,
0.210176015665816, 0.609895236463043, 0.591124373918946,
0.58187326824239, 0.0378568479840844, 0.126892202895244,
0.0152090656484838, 0.0152090656484838, 0.0585326533474545,
0.0585326533474545, 0.0775525505364441, 0.126892202895244,
0.0951622248242906, 0.0585326533474545, 0.0378568479840844,
0.0585326533474545, 0.0152090656484838, 0.0152090656484838,
0.111556439370444, 0.232939774941222, 0.374129156018743,
0.309825356331568, 0.459072793066665, 0.374129156018743,
0.139912664472351, 0.110462748788381, 0.037485702415613,
0.476255040318073, 0.466096963496756, 0.466096963496756,
0.428670275938566, 0.589725250514494, 0.547235252341441,
0.587670283432171, 0.510146385710354, 0.169037347727828,
0.119219651092275, 0.104811166292231, 0.369092608582107,
0.458090402952369, 0.425199566970549, 0.414306966296783,
0.293973826472744, 0.318132945119474, 0.34506362927332, 0.236036906790036,
0.526107066406678, 0.514536911258616, 0.494679026731963,
0.644173648408422, 0.577262524996099, 0.590941947338372,
0.573706922030602, 0.584207479104045, 0.599082800695941,
0.660196138391623, 0.594234162286239, 0.605387349621597,
0.473522453920122, 0.582491482588926, 0.634477243690635,
0.613024542636099, 0.635836394376859, 0.66380955856688, 0.606935930414305,
0.623293981181731, 0.575491834795137, 0.589277366937424,
0.605387349621597, 0.63173374656699, 0.592594151562409, 0.603827901506372,
0.623293981181731, 0.0350783637283718, 0.0585003710820993,
0.0400291247735614, 0.120406949659012, 0.312024255660586,
0.329125958574745, 0.329125958574745, 0.235557836017841,
0.299875798966989, 0.299875798966989, 0.157911017972106,
0.312024255660586, 0.0756020739962579, 0.091523378507426,
0.44700785177329, 0.415249326331636, 0.382807597260579, 0.456691820247472,
0.544930205105051, 0.459831560988572, 0.469002332695363,
0.512569846762844, 0.218732276302281, 0.176580407607362,
0.185700252024063, 0.342978102747015, 0.305616961533692,
0.408867154152023, 0.240542679171189, 0.176580407607362,
0.284170677698895, 0.240542679171189, 0.528783863661652,
0.562072977352849, 0.594884658166908, 0.515792977199508),
color = c("20S", "20S", "20S", "20S", "20S", "20S", "20S",
"20S", "20S", "20S", "20S", "20Y", "20Y", "20Y", "20Y", "20Y",
"20Y", "20Y", "20Y", "20Y", "20Y", "20Y", "20S", "20S", "20S",
"20S", "20S", "20S", "20S", "20Y", "20Y", "20Y", "20Y", "20Y",
"20Y", "20Y", "28S", "28S", "28S", "28S", "28S", "28S", "28S",
"28S", "28S", "28S", "28S", "28S", "28S", "28S", "28S", "28S",
"28S", "28S", "28S", "28S", "28Y", "28Y", "28Y", "28Y", "28Y",
"28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28S", "28S", "28S",
"28S", "28S", "28S", "28S", "28Y", "28Y", "28Y", "28Y", "28Y",
"28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y",
"28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y",
"28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28S",
"25S", "25S", "25S", "25S", "25S", "25S", "25S", "25S", "25S",
"25S", "25S", "25Y", "25Y", "25Y", "25Y", "25Y", "25Y", "25Y",
"25Y", "25Y", "25Y", "25S", "25S", "25S", "25S", "25S", "25S",
"25Y", "25Y", "25Y", "25Y", "25Y", "25Y", "25Y", "25Y")), row.names = c(NA,
-141L), class = c("tbl_df", "tbl", "data.frame"))

Looping to create a new column of counts from specific rows

I'd like to make a new column here in my dataset. I want to pick counts out from the full_name column for the species Stegastes planifrons and make a new column with counts (including transects where the count is 0). I used this code:
Fish1$Stegastes_planifrons <- rep(0, nrow(Fish1))
for (i in 1:nrow(Fish1)) {
if (Fish1$Full_name[i] == "Stegastes planifrons") {
Fish1$Stegastes_planifrons[i] == Fish1$count[i]
}
}
EDIT: Data in dput form:
structure(list(Year = c(2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019), Location = c("Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela"), Site = c("AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD"), Depth = c(10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10), Transect = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), Family = c("Pomacentridae",
"Haemulidae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Serranidae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Haemulidae", "Pomacentridae", "Pomacentridae",
"Acanthuridae", "Haemulidae", "Haemulidae", "Haemulidae", "Scaridae",
"Carangidae", "Scaridae", "Labridae", "Scaridae", "Scaridae",
"Haemulidae", "Scaridae", "Scaridae", "Scaridae", "Scaridae",
"Scaridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Scaridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Scaridae", "Pomacentridae", "Pomacentridae",
"Haemulidae", "Pomacentridae", "Pomacentridae", "Haemulidae",
"Lutjanidae", "Scaridae", "Pomacentridae", "Pomacentridae", "Pomacanthidae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Labridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Labridae", "Chaetodontidae", "Haemulidae", "Haemulidae", "Pomacentridae",
"Labridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Labridae", "Pomacentridae", "Pomacentridae", "Haemulidae", "Haemulidae",
"Pomacentridae"), Genus = c("Stegastes", "Anisotremus", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Serranus", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Microspathodon", "Abudefduf", "Microspathodon",
"Haemulon", "Microspathodon", "Abudefduf", "Acanthurus", "Haemulon",
"Anisotremus", "Anisotremus", "Sparisoma", "Caranx", "Scarus",
"Halichoeres", "Scarus", "Sparisoma", "Anisotremus", "Sparisoma",
"Scarus", "Scarus", "Scarus", "Scarus", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Scarus", "Microspathodon", "Stegastes",
"Stegastes", "Microspathodon", "Microspathodon", "Stegastes",
"Stegastes", "Scarus", "Stegastes", "Stegastes", "Haemulon",
"Stegastes", "Stegastes", "Anisotremus", "Lutjanus", "Scarus",
"Stegastes", "Stegastes", "Holacanthus", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Halichoeres", "Stegastes",
"Stegastes", "Microspathodon", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Thalassoma", "Chaetodon",
"Haemulon", "Haemulon", "Stegastes", "Thalassoma", "Stegastes",
"Stegastes", "Stegastes", "Halichoeres", "Stegastes", "Chromis",
"Haemulon", "Haemulon", "Stegastes"), Species = c("planifrons",
"virginicus", "adustus", "partitus", "partitus", "adustus", "partitus",
"adustus", "adustus", "adustus", "variabilis", "partitus", "adustus",
"adustus", "tortugarum", "adustus", "partitus", "adustus", "partitus",
"chrysurus", "saxatilis", "chrysurus", "carbonarium", "chrysurus",
"saxatilis", "coeruleus", "chrysargyreum", "virginicus", "virginicus",
"viride", "ruber", "vetula", "bivittatus", "iseri", "viride",
"virginicus", "viride", "iseri", "iseri", "iseri", "vetula",
"partitus", "adustus", "adustus", "adustus", "iseri", "chrysurus",
"adustus", "adustus", "chrysurus", "chrysurus", "adustus", "adustus",
"iseri", "diencaeus", "adustus", "sciurus", "adustus", "adustus",
"virginicus", "mahogoni", "vetula", "partitus", "adustus", "tricolor",
"partitus", "adustus", "adustus", "partitus", "partitus", "maculipinna",
"partitus", "variabilis", "chrysurus", "adustus", "variabilis",
"adustus", "partitus", "adustus", "adustus", "partitus", "adustus",
"adustus", "diencaeus", "partitus", "bifasciatum", "capistratus",
"carbonarium", "carbonarium", "adustus", "bifasciatum", "adustus",
"partitus", "diencaeus", "bivittatus", "adustus", "insolata",
"sciurus", "carbonarium", "adustus"), Full_name = c("Stegastes planifrons",
"Anisotremus virginicus", "Stegastes adustus", "Stegastes partitus",
"Stegastes partitus", "Stegastes adustus", "Stegastes partitus",
"Stegastes adustus", "Stegastes adustus", "Stegastes adustus",
"Stegastes variabilis", "Stegastes partitus", "Stegastes adustus",
"Stegastes adustus", "Serranus tortugarum", "Stegastes adustus",
"Stegastes partitus", "Stegastes adustus", "Stegastes partitus",
"Microspathodon chrysurus", "Abudefduf saxatilis", "Microspathodon chrysurus",
"Haemulon carbonarium", "Microspathodon chrysurus", "Abudefduf saxatilis",
"Acanthurus coeruleus", "Haemulon chrysargyreum", "Anisotremus virginicus",
"Anisotremus virginicus", "Sparisoma viride", "Caranx ruber",
"Scarus vetula", "Halichoeres bivittatus", "Scarus iseri", "Sparisoma viride",
"Anisotremus virginicus", "Sparisoma viride", "Scarus iseri",
"Scarus iseri", "Scarus iseri", "Scarus vetula", "Stegastes partitus",
"Stegastes adustus", "Stegastes adustus", "Stegastes adustus",
"Scarus iseri", "Microspathodon chrysurus", "Stegastes adustus",
"Stegastes adustus", "Microspathodon chrysurus", "Microspathodon chrysurus",
"Stegastes adustus", "Stegastes adustus", "Scarus iseri", "Stegastes diencaeus",
"Stegastes adustus", "Haemulon sciurus", "Stegastes adustus",
"Stegastes adustus", "Anisotremus virginicus", "Lutjanus mahogoni",
"Scarus vetula", "Stegastes partitus", "Stegastes adustus", "Holacanthus tricolor",
"Stegastes partitus", "Stegastes adustus", "Stegastes adustus",
"Stegastes partitus", "Stegastes partitus", "Halichoeres maculipinna",
"Stegastes partitus", "Stegastes variabilis", "Microspathodon chrysurus",
"Stegastes adustus", "Stegastes variabilis", "Stegastes adustus",
"Stegastes partitus", "Stegastes adustus", "Stegastes adustus",
"Stegastes partitus", "Stegastes adustus", "Stegastes adustus",
"Stegastes diencaeus", "Stegastes partitus", "Thalassoma bifasciatum",
"Chaetodon capistratus", "Haemulon carbonarium", "Haemulon carbonarium",
"Stegastes adustus", "Thalassoma bifasciatum", "Stegastes adustus",
"Stegastes partitus", "Stegastes diencaeus", "Halichoeres bivittatus",
"Stegastes adustus", "Chromis insolata", "Haemulon sciurus",
"Haemulon carbonarium", "Stegastes adustus"), Guild = c("Omnivore",
"Invertivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Omnivore",
"Herbivore", "Herbivore", "Herbivore", "Planktivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Invertivore",
"Herbivore", "Invertivore", "Herbivore", "Invertivore", "Herbivore",
"Invertivore", "Invertivore", "Invertivore", "Herbivore", "Carnivore",
"Herbivore", "Invertivore", "Herbivore", "Herbivore", "Invertivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Invertivore", "Herbivore", "Herbivore", "Invertivore", "Carnivore",
"Herbivore", "Herbivore", "Herbivore", "Invertivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Invertivore",
"Herbivore", "Omnivore", "Herbivore", "Herbivore", "Omnivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Invertivore",
"Invertivore", "Invertivore", "Invertivore", "Herbivore", "Invertivore",
"Herbivore", "Herbivore", "Herbivore", "Invertivore", "Herbivore",
"Planktivore", "Invertivore", "Invertivore", "Herbivore"), count = c(13,
4, 41, 10, 10, 41, 10, 41, 41, 41, 2, 10, 41, 41, 2, 41, 10,
41, 10, 4, 3, 4, 1, 4, 3, 1, 1, 4, 4, 3, 1, 1, 1, 2, 3, 4, 3,
6, 6, 6, 2, 2, 40, 40, 40, 6, 6, 40, 40, 6, 6, 40, 40, 6, 1,
40, 1, 40, 40, 1, 1, 2, 14, 52, 1, 14, 52, 52, 14, 14, 1, 14,
5, 4, 52, 5, 52, 14, 52, 52, 14, 52, 52, 2, 14, 2, 2, 3, 3, 52,
2, 52, 14, 2, 1, 52, 1, 2, 3, 52), `Transect ID` = c("2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10"
), Stegastes_planifrons = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)), row.names = c(1L, 2L, 3L, 4L, 5L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L,
52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L,
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L,
78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 89L, 90L, 91L,
92L, 93L, 94L, 95L, 96L, 97L, 98L, 104L, 105L, 106L, 107L, 108L,
109L, 110L), class = "data.frame")
Without your data (I can't extract your data from the image you posted), I can't test/troubleshoot this. The best way to share data is to dput(Fish1) and copy/paste the value returned in the console as an edit to your original question. If that is too cumbersome because the dataset is large, you could share a subset of your data or an example dummy dataset. This will get more eyes on your question and will lead to a higher quality answer that's more likely to solve your problem.
Anyways, here is a demonstration of how to summarize counts by group using dplyr. If you still have issues after working through this example, please let me know. I'll help the best I can. Thanks!
data(iris)
library(dplyr)
# Dummy transect and count variable for demo
iris$transect <- sample.int(n = 3, size = nrow(iris), replace = TRUE)
iris$count <- iris$Sepal.Length
virginica _counts <- iris %>%
filter(Species == "virginica") %>%
group_by(transect) %>%
summarize(counts = sum(count))
virginica_counts
#> # A tibble: 3 x 2
#> transect counts
#> <int> <dbl>
#> 1 1 112.
#> 2 2 130.
#> 3 3 87.9
Created on 2022-04-05 by the reprex package (v2.0.1)
EDIT
Using your data:
Fish1$Transect_ID <- Fish1$`Transect ID`
Fish1 %>%
group_by(Transect_ID, .drop = FALSE) %>%
filter(Full_name == "Stegastes planifrons") %>%
summarize(counts = sum(count))
Transect_ID counts
<chr> <dbl>
1 2019_Tela_AD_1_10 13
2 2019_Tela_AD_2_10 0
3 2019_Tela_AD_3_10 0
EDIT 2
Based on the conversation in the comments, I think this is what you have described:
Fish1_Splaniforms <- Fish1 %>%
group_by(Transect_ID, .drop = FALSE) %>%
mutate(S.planiforms_counts = sum(count[Full_name == "Stegastes planifrons"]))
However, if the ultimate plan is for each species to get its own column that indicates counts (of that species) and rows that indicate transects, you could convert from long to wide:
library(reshape2)
Fish1$Transect_ID <- Fish1$`Transect ID`
Fish1_wide <- dcast(Fish1, Transect_ID ~ Full_name, value.var="count", fun.aggregate = sum)
head(Fish1_wide)
#> Transect_ID Abudefduf saxatilis Acanthurus coeruleus
#> 1 2019_Tela_AD_1_10 6 1
#> 2 2019_Tela_AD_2_10 0 0
#> 3 2019_Tela_AD_3_10 0 0
#> Anisotremus virginicus Caranx ruber Chaetodon capistratus Chromis insolata
#> 1 16 1 0 0
#> 2 1 0 0 0
#> 3 0 0 2 1
#> Haemulon carbonarium Haemulon chrysargyreum Haemulon sciurus
#> 1 1 1 0
#> 2 0 0 1
#> 3 9 0 2
#> Halichoeres bivittatus Halichoeres maculipinna Holacanthus tricolor
#> 1 1 0 0
#> 2 0 0 0
#> 3 1 1 1
#> Lutjanus mahogoni Microspathodon chrysurus Scarus iseri Scarus vetula
#> 1 0 12 2 1
#> 2 1 18 30 4
#> 3 0 4 0 0
#> Serranus tortugarum Sparisoma viride Stegastes adustus Stegastes diencaeus
#> 1 2 9 369 0
#> 2 0 0 400 1
#> 3 0 0 676 4
#> Stegastes partitus Stegastes planifrons Stegastes variabilis
#> 1 60 13 2
#> 2 2 0 0
#> 3 126 0 10
#> Thalassoma bifasciatum
#> 1 0
#> 2 0
#> 3 4

group_by (months and years) and summarise_if does not leave single months with the mean values

I am sorry for being vague in my title, but I have no idea how I need to address this.
I have a dataframe of government responses per month per year and I would like to get the average of each column per month.
Now I think that I have the right syntax only my results don't leave me with one single month per year but multiple, and I for the life of me cannot grasp what I am doing wrong.
The code I am running is:
test <- ds %>%
group_by(month, year, C1_calculated, C2_calculated, C3_calculated, C4_calculated, C5_calculated, C6_calculated, C7_calculated, C8_calculated, E1_calculated, E2_calculated) %>%
summarise_if(is.integer, mean) %>%
ungroup
And the results are as follows:
test2 <- structure(list(month = c(1, 1, 1, 2, 2, 2), year = c(2020, 2021,
2021, 2020, 2021, 2021), C1_calculated = c(0, 11.1111111111111,
11.1111111111111, 0, 7.40740740740741, 11.1111111111111), C2_calculated = c(0,
11.1111111111111, 11.1111111111111, 0, 11.1111111111111, 11.1111111111111
), C3_calculated = c(0, 11.1111111111111, 11.1111111111111, 0,
11.1111111111111, 11.1111111111111), C4_calculated = c(0, 11.1111111111111,
11.1111111111111, 0, 11.1111111111111, 11.1111111111111), C5_calculated = c(0,
5.55555555555556, 5.55555555555556, 0, 5.55555555555556, 5.55555555555556
), C6_calculated = c(0, 3.7037037037037, 7.40740740740741, 0,
7.40740740740741, 7.40740740740741), C7_calculated = c(0, 5.55555555555556,
5.55555555555556, 0, 5.55555555555556, 5.55555555555556), C8_calculated = c(0,
9.72222222222222, 9.72222222222222, 0, 9.72222222222222, 9.72222222222222
), E1_calculated = c(0, 50, 50, 0, 50, 50), E2_calculated = c(0,
37.5, 37.5, 0, 37.5, 37.5)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
As you can see I have multiple instances of the same month per year which doesn't really help.
Here is a random sample of 20 rows of my data set:
ds <- structure(list(C1_calculated = c(3.7037037037037, 7.40740740740741,
3.7037037037037, 7.40740740740741, 3.7037037037037, 3.7037037037037,
11.1111111111111, 11.1111111111111, 3.7037037037037, 7.40740740740741,
3.7037037037037, 3.7037037037037, 3.7037037037037, 3.7037037037037,
11.1111111111111, 7.40740740740741, 3.7037037037037, 7.40740740740741,
0, 7.40740740740741), C2_calculated = c(7.40740740740741, 7.40740740740741,
7.40740740740741, 11.1111111111111, 7.40740740740741, 7.40740740740741,
7.40740740740741, 7.40740740740741, 7.40740740740741, 7.40740740740741,
7.40740740740741, 3.7037037037037, 7.40740740740741, 7.40740740740741,
7.40740740740741, 7.40740740740741, 7.40740740740741, 11.1111111111111,
0, 7.40740740740741), C3_calculated = c(0, 11.1111111111111,
11.1111111111111, 11.1111111111111, 5.55555555555556, 2.77777777777778,
11.1111111111111, 11.1111111111111, 5.55555555555556, 11.1111111111111,
11.1111111111111, 5.55555555555556, 0, 11.1111111111111, 11.1111111111111,
11.1111111111111, 11.1111111111111, 11.1111111111111, 0, 11.1111111111111
), C4_calculated = c(5.55555555555556, 11.1111111111111, 11.1111111111111,
11.1111111111111, 0, 11.1111111111111, 11.1111111111111, 11.1111111111111,
0, 11.1111111111111, 11.1111111111111, 0, 5.55555555555556, 11.1111111111111,
11.1111111111111, 8.33333333333333, 11.1111111111111, 11.1111111111111,
0, 8.33333333333333), C5_calculated = c(0, 5.55555555555556,
5.55555555555556, 5.55555555555556, 5.55555555555556, 0, 5.55555555555556,
5.55555555555556, 0, 5.55555555555556, 5.55555555555556, 0, 0,
5.55555555555556, 5.55555555555556, 0, 5.55555555555556, 5.55555555555556,
0, 0), C6_calculated = c(3.7037037037037, 3.7037037037037, 3.7037037037037,
7.40740740740741, 0, 3.7037037037037, 7.40740740740741, 7.40740740740741,
0, 7.40740740740741, 3.7037037037037, 0, 3.7037037037037, 3.7037037037037,
7.40740740740741, 3.7037037037037, 7.40740740740741, 7.40740740740741,
0, 3.7037037037037), C7_calculated = c(0, 5.55555555555556, 5.55555555555556,
5.55555555555556, 0, 0, 5.55555555555556, 5.55555555555556, 0,
5.55555555555556, 5.55555555555556, 0, 0, 5.55555555555556, 5.55555555555556,
5.55555555555556, 5.55555555555556, 5.55555555555556, 0, 5.55555555555556
), C8_calculated = c(8.33333333333333, 8.33333333333333, 9.72222222222222,
9.72222222222222, 9.72222222222222, 8.33333333333333, 8.33333333333333,
8.33333333333333, 9.72222222222222, 9.72222222222222, 9.72222222222222,
9.72222222222222, 8.33333333333333, 9.72222222222222, 8.33333333333333,
8.33333333333333, 9.72222222222222, 9.72222222222222, 0, 8.33333333333333
), E1_calculated = c(50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50, 0, 50), E2_calculated = c(62.5,
62.5, 37.5, 37.5, 12.5, 37.5, 62.5, 62.5, 37.5, 37.5, 37.5, 37.5,
62.5, 37.5, 62.5, 62.5, 37.5, 37.5, 0, 62.5), month = c(8, 5,
5, 2, 8, 9, 5, 4, 7, 4, 6, 7, 8, 6, 5, 6, 4, 2, 1, 6), year = c(2020,
2020, 2021, 2021, 2021, 2020, 2020, 2020, 2021, 2021, 2021, 2021,
2020, 2021, 2020, 2020, 2021, 2021, 2020, 2020)), row.names = c(227L,
145L, 488L, 423L, 586L, 272L, 122L, 104L, 565L, 464L, 520L, 555L,
224L, 539L, 124L, 157L, 482L, 417L, 14L, 162L), class = "data.frame")
Thanks in advance for all your help!
Yours data:
test2 <- structure(list(month = c(1, 1, 1, 2, 2, 2), year = c(2020, 2021,
2021, 2020, 2021, 2021), C1_calculated = c(0, 11.1111111111111,
11.1111111111111, 0, 7.40740740740741, 11.1111111111111), C2_calculated = c(0,
11.1111111111111, 11.1111111111111, 0, 11.1111111111111, 11.1111111111111
), C3_calculated = c(0, 11.1111111111111, 11.1111111111111, 0,
11.1111111111111, 11.1111111111111), C4_calculated = c(0, 11.1111111111111,
11.1111111111111, 0, 11.1111111111111, 11.1111111111111), C5_calculated = c(0,
5.55555555555556, 5.55555555555556, 0, 5.55555555555556, 5.55555555555556
), C6_calculated = c(0, 3.7037037037037, 7.40740740740741, 0,
7.40740740740741, 7.40740740740741), C7_calculated = c(0, 5.55555555555556,
5.55555555555556, 0, 5.55555555555556, 5.55555555555556), C8_calculated = c(0,
9.72222222222222, 9.72222222222222, 0, 9.72222222222222, 9.72222222222222
), E1_calculated = c(0, 50, 50, 0, 50, 50), E2_calculated = c(0,
37.5, 37.5, 0, 37.5, 37.5)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
Summarise:
library(tidyverse)
test2 %>%
group_by(month, year) %>%
summarise_at(vars(starts_with("C")), mean)
output
# A tibble: 4 x 10
# Groups: month [2]
month year C1_calculated C2_calculated C3_calculated C4_calculated C5_calculated C6_calculated C7_calculated C8_calculated
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 2020 0 0 0 0 0 0 0 0
2 1 2021 11.1 11.1 11.1 11.1 5.56 5.56 5.56 9.72
3 2 2020 0 0 0 0 0 0 0 0
4 2 2021 9.26 11.1 11.1 11.1 5.56 7.41 5.56 9.72

How to delete all following rows once an observation/condition of interest has reached in the previous row R (right censoring)

Hope you can help with what I believe is a dplyr/tidyr problem:
QUESTION
How do I conditionally delete following observations once the first observation of interest was made? (right-censoring)
DATA
structure(list(record_id = c(120321, 120321, 120321, 120321,
120321, 120321, 120321, 120321, 120321, 120321, 120321, 120321,
120321, 120321, 120321, 120321, 120321, 120321, 120321, 120321,
120321), day_count = c(579, 580, 581, 582, 583, 584, 585, 586,
587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599
), day_count_stop = c(580, 581, 582, 583, 584, 585, 586, 587,
588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600
), te_yn = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0)), row.names = c(NA, -21L), groups = structure(list(
record_id = 120321, .rows = structure(list(1:21), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -1L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
EXPLANATION
So I actually followed record_id 120321 from day 0 to day 732. In this example you see the days of follow-up from day 579 to 600.
I want to delete all rows after the day te_yn has become 1. That is the observation of interest. So at day 584, te_yn became 1. I want to delete day 585 until the end.
Best Wishes
This is part of a larger problem on survival analysis. If one is experienced with Cox PH analysis and would like to help, please view the link below:
Coding help for Cox PH analysis with a categorical time-dependent variable
So, I assume you'd have to do so for different record_id. Here I am grouping by that. See if this works for you.
> library(dplyr)
> df %>% group_by(record_id) %>% filter(if(1 %in% te_yn) row_number() == 1: (which(te_yn == 1) -1) else TRUE)
# A tibble: 15 x 4
# Groups: record_id [2]
record_id day_count day_count_stop te_yn
<dbl> <dbl> <dbl> <dbl>
1 120321 579 580 0
2 120321 580 581 0
3 120321 581 582 0
4 120321 582 583 0
5 120321 583 584 0
6 120322 579 580 0
7 120322 580 581 0
8 120322 581 582 0
9 120322 582 583 0
10 120322 583 584 0
11 120322 584 586 0
12 120322 585 587 0
13 120322 586 588 0
14 120322 587 589 0
15 120322 588 590 0
>
Data used:
structure(list(record_id = c(120321, 120321, 120321, 120321,
120321, 120321, 120321, 120321, 120321, 120321, 120321, 120321,
120321, 120321, 120321, 120321, 120321, 120321, 120321, 120321,
120321, 120322, 120322, 120322, 120322, 120322, 120322, 120322,
120322, 120322, 120322), day_count = c(579, 580, 581, 582, 583,
584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596,
597, 598, 599, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588
), day_count_stop = c(580, 581, 582, 583, 584, 585, 586, 587,
588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600,
580, 581, 582, 583, 584, 586, 587, 588, 589, 590), te_yn = c(0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -31L), class = c("tbl_df",
"tbl", "data.frame"))
Have added additional group to the original data.

Match data from two different data frames in R like Excel VLOOKUP

I am trying to pull data from one data frame to another based on the equivalent of a VLOOKUP table in Excel. I have had a look at the most popular VLOOKUP question in R, but I cannot see how it applies to my specific problem. The key thing is that I don't want to pull all of the columns from the second data frame into my first one - I only want to pull in one column. I'm pretty sure this will be some kind of derivation of a merge function.
Referring to the below data, I am trying to create a new column called df1$Trait1Percentile. This needs to draw from LookupTable$Trait1Percentiles based on a match between df1$Trait1Scores and
LookupTable$Scores.
#Import data.
df1 <- structure(list(JobNumber = c(634L, 21L, 300L, 797L, 1112L, 147L,
1L, 4L, 260L, 194L, 981L, 1110L, 634L, 554L, 213L, 722L, 1036L,
855L, 624L, 1113L, 681L, 547L, 195L, 624L, 546L, 201L, 918L,
1069L, 300L, 294L, 587L, 933L, 918L, 620L, 918L, 298L, 749L,
295L, 635L, 515L, 624L, 147L, 200L, 527L, 800L, 827L, 4L, 568L,
252L, 655L, 559L, 629L, 639L, 933L, 214L, 750L, 1066L, 495L,
1113L, 1L, 1113L, 12L, 561L, 741L, 495L, 981L, 147L, 199L, 629L,
163L, 615L, 294L, 49L, 624L, 260L, 1L, 299L, 193L, 108L, 113L,
426L, 299L, 708L, 749L, 749L, 483L, 935L, 1036L, 295L, 12L, 1113L,
1038L, 4L, 973L, 448L, 295L, 197L, 76L, 1L, 1L), Trait1Score = c(3.89,
4.39, 4.22, 4.21, 3.94, 3.9, 4.58, 4.5, 4.29, 4.47, 4.41, 4.4,
4.14, 4.78, 4.09, 4.58, 4.27, 4.24, 3.96, 3.94, 4.3, 4.07, 4.28,
4.19, 4.57, 4.74, 3.29, 4.23, 3.51, 3.77, 4.46, 5.04, 4.25, 3.92,
3.78, 4.43, 4.12, 4.18, 4.63, 3.25, 3.87, 4.4, 3.83, 4.03, 3.42,
4.9, 4.09, 4.58, 4.29, 4.7, 4.38, 4.61, 4.41, 4.5, 4.6, 4.22,
3.72, 4.34, 4.34, 4.38, 4.15, 4.22, 3.93, 5, 3.81, 4.3, 4.6,
4.96, 4.29, 4.8, 5.05, 3.76, 4.81, 4.77, 4.25, 4.17, 4.75, 4.15,
4.35, 4.23, 5.31, 4.18, 3.67, 3.84, 4.06, 3.66, 3.58, 4.37, 4.43,
4.63, 4.74, 4.79, 5.04, 3.55, 3.64, 4.9, 4.38, 4.01, 4.47, 4.53
), Trait2Score = c(4, 2.94, 3.17, 3.83, 4.22, 3.83, 5.11, 3,
2.83, 2.78, 2.22, 2.22, 4.11, 2.39, 2.22, 2.06, 2.89, 3.61, 3.89,
4.89, 3.78, 4.22, 4.5, 4.39, 1.89, 4.78, 4.56, 3.78, 2.28, 4.61,
2.72, 1.89, 4.44, 4.06, 3.72, 2.44, 3.61, 2.06, 2.17, 6.44, 3.22,
2.78, 4.61, 2.72, 2.83, 2.44, 6.5, 2.28, 2.89, 2.11, 4.44, 2.83,
3, 6.33, 3.11, 3.17, 3.67, 4.5, 2.5, 4.33, 5, 2.89, 3.89, 1.72,
3.33, 4.28, 2.17, 3.17, 2.61, 2.89, 1.22, 3.39, 1.28, 2.61, 2.5,
4.56, 2.89, 4.89, 3.11, 3.5, 1.44, 2.39, 5.33, 3.78, 1.5, 3.44,
5.83, 3.17, 3.78, 2.67, 1.61, 1.83, 4.56, 4.67, 4.61, 2.5, 4.94,
3.94, 4.33, 2.72)), row.names = c(NA, -100L), class = "data.frame")
LookupTable <- structure(list(Scores = c(0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06,
0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17,
0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28,
0.29, 0.3, 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39,
0.4, 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5,
0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6, 0.61,
0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7, 0.71, 0.72,
0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8, 0.81, 0.82, 0.83,
0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9, 0.91, 0.92, 0.93, 0.94,
0.95, 0.96, 0.97, 0.98, 0.99, 1, 1.01, 1.02, 1.03, 1.04, 1.05,
1.06, 1.07, 1.08, 1.09, 1.1, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16,
1.17, 1.18, 1.19, 1.2, 1.21, 1.22, 1.23, 1.24, 1.25, 1.26, 1.27,
1.28, 1.29, 1.3, 1.31, 1.32, 1.33, 1.34, 1.35, 1.36, 1.37, 1.38,
1.39, 1.4, 1.41, 1.42, 1.43, 1.44, 1.45, 1.46, 1.47, 1.48, 1.49,
1.5, 1.51, 1.52, 1.53, 1.54, 1.55, 1.56, 1.57, 1.58, 1.59, 1.6,
1.61, 1.62, 1.63, 1.64, 1.65, 1.66, 1.67, 1.68, 1.69, 1.7, 1.71,
1.72, 1.73, 1.74, 1.75, 1.76, 1.77, 1.78, 1.79, 1.8, 1.81, 1.82,
1.83, 1.84, 1.85, 1.86, 1.87, 1.88, 1.89, 1.9, 1.91, 1.92, 1.93,
1.94, 1.95, 1.96, 1.97, 1.98, 1.99, 2, 2.01, 2.02, 2.03, 2.04,
2.05, 2.06, 2.07, 2.08, 2.09, 2.1, 2.11, 2.12, 2.13, 2.14, 2.15,
2.16, 2.17, 2.18, 2.19, 2.2, 2.21, 2.22, 2.23, 2.24, 2.25, 2.26,
2.27, 2.28, 2.29, 2.3, 2.31, 2.32, 2.33, 2.34, 2.35, 2.36, 2.37,
2.38, 2.39, 2.4, 2.41, 2.42, 2.43, 2.44, 2.45, 2.46, 2.47, 2.48,
2.49, 2.5, 2.51, 2.52, 2.53, 2.54, 2.55, 2.56, 2.57, 2.58, 2.59,
2.6, 2.61, 2.62, 2.63, 2.64, 2.65, 2.66, 2.67, 2.68, 2.69, 2.7,
2.71, 2.72, 2.73, 2.74, 2.75, 2.76, 2.77, 2.78, 2.79, 2.8, 2.81,
2.82, 2.83, 2.84, 2.85, 2.86, 2.87, 2.88, 2.89, 2.9, 2.91, 2.92,
2.93, 2.94, 2.95, 2.96, 2.97, 2.98, 2.99, 3, 3.01, 3.02, 3.03,
3.04, 3.05, 3.06, 3.07, 3.08, 3.09, 3.1, 3.11, 3.12, 3.13, 3.14,
3.15, 3.16, 3.17, 3.18, 3.19, 3.2, 3.21, 3.22, 3.23, 3.24, 3.25,
3.26, 3.27, 3.28, 3.29, 3.3, 3.31, 3.32, 3.33, 3.34, 3.35, 3.36,
3.37, 3.38, 3.39, 3.4, 3.41, 3.42, 3.43, 3.44, 3.45, 3.46, 3.47,
3.48, 3.49, 3.5, 3.51, 3.52, 3.53, 3.54, 3.55, 3.56, 3.57, 3.58,
3.59, 3.6, 3.61, 3.62, 3.63, 3.64, 3.65, 3.66, 3.67, 3.68, 3.69,
3.7, 3.71, 3.72, 3.73, 3.74, 3.75, 3.76, 3.77, 3.78, 3.79, 3.8,
3.81, 3.82, 3.83, 3.84, 3.85, 3.86, 3.87, 3.88, 3.89, 3.9, 3.91,
3.92, 3.93, 3.94, 3.95, 3.96, 3.97, 3.98, 3.99, 4, 4.01, 4.02,
4.03, 4.04, 4.05, 4.06, 4.07, 4.08, 4.09, 4.1, 4.11, 4.12, 4.13,
4.14, 4.15, 4.16, 4.17, 4.18, 4.19, 4.2, 4.21, 4.22, 4.23, 4.24,
4.25, 4.26, 4.27, 4.28, 4.29, 4.3, 4.31, 4.32, 4.33, 4.34, 4.35,
4.36, 4.37, 4.38, 4.39, 4.4, 4.41, 4.42, 4.43, 4.44, 4.45, 4.46,
4.47, 4.48, 4.49, 4.5, 4.51, 4.52, 4.53, 4.54, 4.55, 4.56, 4.57,
4.58, 4.59, 4.6, 4.61, 4.62, 4.63, 4.64, 4.65, 4.66, 4.67, 4.68,
4.69, 4.7, 4.71, 4.72, 4.73, 4.74, 4.75, 4.76, 4.77, 4.78, 4.79,
4.8, 4.81, 4.82, 4.83, 4.84, 4.85, 4.86, 4.87, 4.88, 4.89, 4.9,
4.91, 4.92, 4.93, 4.94, 4.95, 4.96, 4.97, 4.98, 4.99, 5, 5.01,
5.02, 5.03, 5.04, 5.05, 5.06, 5.07, 5.08, 5.09, 5.1, 5.11, 5.12,
5.13, 5.14, 5.15, 5.16, 5.17, 5.18, 5.19, 5.2, 5.21, 5.22, 5.23,
5.24, 5.25, 5.26, 5.27, 5.28, 5.29, 5.3, 5.31, 5.32, 5.33, 5.34,
5.35, 5.36, 5.37, 5.38, 5.39, 5.4, 5.41, 5.42, 5.43, 5.44, 5.45,
5.46, 5.47, 5.48, 5.49, 5.5, 5.51, 5.52, 5.53, 5.54, 5.55, 5.56,
5.57, 5.58, 5.59, 5.6, 5.61, 5.62, 5.63, 5.64, 5.65, 5.66, 5.67,
5.68, 5.69, 5.7, 5.71, 5.72, 5.73, 5.74, 5.75, 5.76, 5.77, 5.78,
5.79, 5.8, 5.81, 5.82, 5.83, 5.84, 5.85, 5.86, 5.87, 5.88, 5.89,
5.9, 5.91, 5.92, 5.93, 5.94, 5.95, 5.96, 5.97, 5.98, 5.99, 6,
6.01, 6.02, 6.03, 6.04, 6.05, 6.06, 6.07, 6.08, 6.09, 6.1, 6.11,
6.12, 6.13, 6.14, 6.15, 6.16, 6.17, 6.18, 6.19, 6.2, 6.21, 6.22,
6.23, 6.24, 6.25, 6.26, 6.27, 6.28, 6.29, 6.3, 6.31, 6.32, 6.33,
6.34, 6.35, 6.36, 6.37, 6.38, 6.39, 6.4, 6.41, 6.42, 6.43, 6.44,
6.45, 6.46, 6.47, 6.48, 6.49, 6.5, 6.51, 6.52, 6.53, 6.54, 6.55,
6.56, 6.57, 6.58, 6.59, 6.6, 6.61, 6.62, 6.63, 6.64, 6.65, 6.66,
6.67, 6.68, 6.69, 6.7, 6.71, 6.72, 6.73, 6.74, 6.75, 6.76, 6.77,
6.78, 6.79, 6.8, 6.81, 6.82, 6.83, 6.84, 6.85, 6.86, 6.87, 6.88,
6.89, 6.9, 6.91, 6.92, 6.93, 6.94, 6.95, 6.96, 6.97, 6.98, 6.99,
7), Trait1Percentiles = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03,
0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.13, 0.13, 0.13,
0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.16, 0.19, 0.19, 0.19, 0.19,
0.25, 0.28, 0.35, 0.38, 0.41, 0.5, 0.5, 0.5, 0.5, 0.54, 0.57,
0.57, 0.6, 0.6, 0.66, 0.66, 0.69, 0.69, 0.85, 0.85, 0.91, 1.01,
1.04, 1.07, 1.1, 1.13, 1.2, 1.23, 1.32, 1.42, 1.48, 1.48, 1.67,
1.73, 1.89, 1.98, 2.14, 2.14, 2.14, 2.33, 2.33, 2.52, 2.52, 2.77,
2.77, 3.12, 3.34, 3.46, 3.75, 3.97, 4.16, 4.57, 4.82, 5.1, 5.26,
5.45, 5.61, 5.73, 6.14, 6.36, 6.65, 7.09, 7.43, 7.43, 8.31, 8.31,
9.01, 9.01, 9.51, 9.51, 10.65, 11.15, 11.69, 12.03, 12.6, 13.39,
14.08, 14.61, 14.96, 15.59, 16.5, 17.23, 18.02, 18.8, 19.78,
20.79, 21.57, 22.33, 22.93, 22.93, 25.01, 25.92, 26.9, 26.9,
28.79, 29.83, 31.28, 32.35, 33.45, 34.43, 35.43, 36.91, 37.95,
39.31, 40.88, 42.05, 43.15, 44.22, 45.61, 46.87, 48.22, 49.23,
50.77, 52.03, 52.03, 54.46, 55.81, 56.94, 56.94, 59.37, 60.66,
61.95, 61.95, 64.28, 65.48, 66.96, 68, 68.79, 69.73, 70.55, 71.43,
72.44, 73.48, 74.3, 74.99, 75.81, 76.76, 77.73, 78.46, 78.46,
80.16, 80.79, 81.64, 81.64, 83.28, 83.97, 84.63, 84.63, 85.76,
86.27, 86.99, 87.46, 87.91, 88.35, 88.69, 88.91, 89.32, 89.73,
90.11, 90.43, 90.8, 91.24, 91.5, 91.69, 91.69, 92.44, 92.72,
93.2, 93.2, 93.76, 93.92, 94.17, 94.17, 94.52, 94.83, 95.21,
95.46, 95.72, 95.87, 95.94, 96.19, 96.35, 96.6, 96.85, 96.98,
97.1, 97.1, 97.2, 97.32, 97.32, 97.54, 97.64, 97.76, 97.76, 97.92,
97.98, 98.05, 98.05, 98.24, 98.3, 98.36, 98.39, 98.55, 98.58,
98.68, 98.77, 98.83, 98.87, 98.87, 98.99, 99.06, 99.06, 99.06,
99.06, 99.06, 99.06, 99.15, 99.15, 99.15, 99.28, 99.28, 99.31,
99.31, 99.31, 99.5, 99.53, 99.59, 99.62, 99.62, 99.65, 99.65,
99.65, 99.65, 99.69, 99.69, 99.72, 99.78, 99.78, 99.81, 99.81,
99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81,
99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.87,
99.87, 99.91, 99.91, 99.94, 99.94, 99.97, 99.97, 99.97, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100), Trait2Percentiles = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.03, 0.03, 0.03,
0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.06, 0.06, 0.06, 0.09, 0.09,
0.09, 0.09, 0.13, 0.13, 0.13, 0.13, 0.19, 0.19, 0.19, 0.22, 0.25,
0.28, 0.31, 0.31, 0.31, 0.41, 0.41, 0.41, 0.41, 0.41, 0.41, 0.57,
0.6, 0.63, 0.69, 0.76, 0.82, 0.82, 0.91, 0.98, 0.98, 1.13, 1.23,
1.23, 1.35, 1.51, 1.54, 1.57, 1.67, 1.67, 1.89, 1.89, 2.08, 2.08,
2.55, 2.55, 2.93, 3.02, 3.15, 3.34, 3.53, 3.94, 4.19, 4.6, 4.85,
5.04, 5.35, 5.57, 6.02, 6.27, 6.65, 7.02, 7.56, 8.06, 8.06, 8.85,
8.85, 9.7, 9.7, 10.55, 10.55, 11.62, 12.38, 12.98, 13.64, 14.3,
14.93, 15.62, 16.35, 17.35, 18.11, 19.15, 20.19, 21.39, 22.52,
23.5, 24.85, 25.61, 26.68, 26.68, 29.13, 29.13, 31.78, 31.78,
34.2, 34.2, 36.91, 38.33, 39.53, 41.35, 42.93, 44.72, 46.52,
48, 49.57, 50.96, 52.41, 54.27, 55.69, 57.64, 58.96, 60.85, 62.2,
64, 65.48, 65.48, 68.31, 69.86, 71.21, 71.21, 73.73, 75.02, 76.54,
77.45, 78.46, 79.78, 80.63, 81.8, 82.8, 83.81, 84.91, 85.86,
86.68, 87.4, 88.16, 88.54, 89.04, 89.86, 90.52, 90.93, 90.93,
91.81, 92.16, 92.47, 92.47, 93.48, 93.8, 94.27, 94.27, 94.68,
95.02, 95.37, 95.59, 95.94, 96.13, 96.41, 96.66, 96.76, 96.79,
97.01, 97.13, 97.32, 97.45, 97.51, 97.57, 97.57, 97.57, 97.95,
98.05, 98.05, 98.24, 98.36, 98.39, 98.39, 98.61, 98.61, 98.74,
98.77, 98.87, 98.93, 98.99, 99.02, 99.12, 99.21, 99.31, 99.4,
99.43, 99.53, 99.59, 99.59, 99.59, 99.59, 99.62, 99.65, 99.65,
99.65, 99.75, 99.75, 99.75, 99.78, 99.81, 99.84, 99.87, 99.87,
99.87, 99.87, 99.87, 99.87, 99.91, 99.91, 99.91, 99.94, 99.94,
99.94, 99.94, 99.94, 99.94, 99.94, 99.97, 99.97, 99.97, 99.97,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100)), class = "data.frame", row.names = c(NA, -701L))
You can use match here :
df1$Trait1Percentile <- LookupTable$Trait1Percentiles[match(df1$Trait1Score, LookupTable$Scores)]
head(df1)
# JobNumber Trait1Score Trait2Score Trait1Percentile
#1 634 3.89 4.00 14.08
#2 21 4.39 2.94 68.00
#3 300 4.22 3.17 46.87
#4 797 4.21 3.83 45.61
#5 1112 3.94 4.22 17.23
#6 147 3.90 3.83 14.61
With merge you need to select relevant columns
merge(df1, LookupTable, by.x = 'Trait1Score', by.y = 'Scores')[1:4]
Similarly in dplyr :
library(dplyr)
inner_join(df1, LookupTable, by = c('Trait1Score' = 'Scores')) %>% select(1:4)

Resources