I build a function that read 2 dataframes that are in differents directories.
First dataframe is biological data and contain a 6 variables (ciclo, name, validated_1, validated_2, automatic_1 and automatic_2). The column name always will be used, but their "name" could be changed in future. Therefore it needs to be called as argument in the function. Similarly columns validated_1 untill automatic_2 could be called. The column name consists in date time and column 2 consists in a category.
Second dataframe is an environmental data and it has the columns year, month, day, hour, min and seg, in this order, that will joined to build a column datetime (similar to column name in biological data) and the column with environmental variable (ex.: temperature)
I build the function mainly in tidyverse that join this datasets and add some datetime as my needs. However I would like to rewrite this function in data.table or base R to see if it improves the time for execution and clean some steps are unnecessary.
My function:
Myfunc_2 <- function(parti_path, parti_data,
ambi_path, ambi_data,
var_datetime = 'name', var = 'validated_2',
var_parti, var_ambi,
periodo = "30 min") {
cycles <- tibble(cycle_rounded = seq(ymd_hm("2020-11-1 0:00"),
ymd_hm("2022-06-10 23:59"),
by = periodo))
setwd(parti_path)
particulas <- fread(parti_data, select = c(var_datetime, var)) %>%
as_tibble()
particulas <- particulas %>%
select(everything()) %>%
filter(eval(particulas[[var]], particulas) %in% var_parti)
particulas <- particulas %>%
transmute(date_time = str_sub(particulas[[1]], 1, 23) %>% ymd_hms()) %>%
transmute(cycle_rounded = round_date(date_time, periodo)) %>%
group_by(cycle_rounded = floor_date(cycle_rounded, periodo)) %>%
summarize(n = n()) %>%
ungroup()
particulas <- full_join(cycles, particulas) %>%
mutate(n = replace_na(n, 0))
setwd(ambi_path)
ambiental <- fread(ambi_data,
select = c('year', 'month', 'day',
'hour', 'min', 'seg', var_ambi)) %>%
as_tibble()
ambiental <- ambiental %>%
transmute(date_time = make_datetime(year, month, day, hour, min, seg),
cycle_rounded = round_date(date_time, periodo), ambiental[, 7]) %>%
select(-date_time) %>%
group_by(cycle_rounded) %>%
summarize(across(everything(), mean, .names = "Mean_{.col}")) %>%
ungroup()
ambiental <- full_join(ambiental, cycles)
dados_ambi_part <- full_join(ambiental, particulas)
}
Part of my environmental data:
structure(list(year = c(2020L, 2020L, 2020L, 2020L, 2020L, 2020L,
2020L, 2020L, 2020L, 2020L, 2022L, 2022L, 2022L, 2022L, 2022L,
2022L, 2022L, 2022L, 2022L, 2022L), month = c(11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L), day = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L), hour = c(11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L), min = c(3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 48L, 48L, 48L, 48L, 48L, 48L, 48L, 48L, 48L,
48L), seg = c(58.91, 59.91, 0.91, 1.91, 2.91, 3.91, 4.91, 5.91,
6.91, 7.91, 12.31, 13.31, 14.31, 15.31, 16.31, 17.31, 18.31,
19.31, 20.31, 21.31), `Condutividade_(mS/cm)` = c(56.257, 56.258,
56.256, 56.256, 56.267, 56.265, 56.265, 56.265, 56.262, 56.259,
58.566, 58.56, 58.557, 58.56, 58.56, 58.561, 58.564, 58.563,
58.566, 58.565), `Temperatura_(°C)` = c(26.251, 26.252, 26.243,
26.249, 26.259, 26.259, 26.258, 26.26, 26.261, 26.251, 29.28,
29.275, 29.277, 29.279, 29.278, 29.279, 29.279, 29.279, 29.279,
29.28)), row.names = c(NA, -20L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x560bd8a7add0>)
Part of my biological data:
structure(list(ciclo = c("2020-12-01 00_34_52.441983", "2020-12-01 00_34_52.441983",
"2020-12-01 00_34_52.441983", "2020-12-01 00_34_52.441983", "2020-12-01 00_34_52.441983",
"2020-12-01 00_34_52.441983", "2020-12-01 00_34_52.441983", "2020-12-01 00_34_52.441983",
"2020-12-01 00_34_52.441983", "2020-12-01 00_34_52.441983", "2020-12-04 18_17_58.115574",
"2020-12-04 18_17_58.115574", "2020-12-04 18_17_58.115574", "2020-12-04 18_17_58.115574",
"2020-12-04 18_17_58.115574", "2020-12-04 18_17_58.115574", "2020-12-04 18_17_58.115574",
"2020-12-04 18_17_58.115574", "2020-12-04 18_17_58.115574", "2020-12-04 18_17_58.115574"
), name = c("2020-12-01_00_34_54.029_1009_1943", "2020-12-01_00_34_54.029_1025_394",
"2020-12-01_00_34_54.029_1345_631", "2020-12-01_00_34_54.029_1360_1538",
"2020-12-01_00_34_54.029_1435_1340", "2020-12-01_00_34_54.029_1508_352",
"2020-12-01_00_34_54.029_154_2219", "2020-12-01_00_34_54.029_1575_1729",
"2020-12-01_00_34_54.029_1744_2288", "2020-12-01_00_34_54.029_1793_1376",
"2020-12-04_18_19_41.811_3711_203", "2020-12-04_18_19_41.811_3751_918",
"2020-12-04_18_19_41.811_3890_1257", "2020-12-04_18_19_41.811_3964_2638",
"2020-12-04_18_19_41.811_3966_800", "2020-12-04_18_19_41.811_3997_34",
"2020-12-04_18_19_41.811_4005_1255", "2020-12-04_18_19_41.811_44_2810",
"2020-12-04_18_19_41.811_502_1551", "2020-12-04_18_19_41.811_520_204"
), validated_1 = c("sombra", "sombra", "sombra", "sombra", "sombra",
"sombra", "sombra", "sombra", "sombra", "sombra", "sombra", "sombra",
"sombra", "organismos", "sombra", "sombra", "sombra", "sombra",
"sombra", "sombra"), validated_2 = c("sombra", "sombra", "sombra",
"sombra", "sombra", "sombra", "sombra", "sombra", "sombra", "sombra",
"sombra", "sombra", "sombra", "organismos", "sombra", "sombra",
"sombra", "sombra", "sombra", "sombra"), automatic_1 = c("organismos",
"sombra", "sombra", "sombra", "sombra", "sombra", "sombra", "sombra",
"organismos", "sombra", "organismos", "sombra", "sombra", "organismos",
"sombra", "sombra", "organismos", "sombra", "sombra", "sombra"
), automatic_2 = c("organismos", "sombra", "sombra", "sombra",
"sombra", "sombra", "sombra", "sombra", "organismos", "sombra",
"organismos", "sombra", "sombra", "organismos", "sombra", "sombra",
"organismos", "sombra", "sombra", "sombra")), row.names = c(NA,
-20L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x560bd8a7add0>)
For call my function:
dados_ambi_part <- Myfunc_2(parti_path, parti_data,
ambi_path, ambi_data,
var_datetime = 'name', var = 'validated_2',
var_parti = "sombra", var_ambi = 'Temperatura_(°C)',
periodo = '30 min')
OBS: parti_path, parti_data, ambi_path and ambi_data I declared previously out of the function;
Thanks any help