Hi All,
First of all I'm fairly new on R, so please have some patience.
I'm trying to a "data structure" (a data frame) that consists of N+1 columns, where the last column contains lists of each row changes (considered only the first N).
In the example below is presented what I have done so far. I have divided the "code" in "Initialization" and "Operation" phases, where I start by building the general structure and then update it as required.
I have two questions:
- When there are several rows that requires update, I was unable to find a vectorized way that wokes (like using
apply
ordo.call
family functions). Afor
cycle was the only solution. How can I implement a vectorized solution? - (Hopefully) You understand the goal of this solution. Can you identify similar (better) solutions to keep track of changes in a dataframe?
Thank you very much for your help.
Best Regards
AC
# Setup/Initialization Phase
base_var <- tribble(
~ErrID, ~Validation, ~Status, ~Dependency, ~CODE,
1, "2020-10-28 09:41:05", "Warnings" , "{QRA3.-[ENTR]}{Q7.19-[HNGZ027]}", 42390,
2, "2020-10-28 09:41:05", "Warnings" , "{Q9.16-[HI0600]}", 42390 ,
3, "2020-10-28 09:41:05", "Warnings" , "{Q9.24-[HIZ040_1]}", 42390,
4, "2020-10-28 09:41:05", "Warnings" , "{Q9.24-[HIZ040_1]}", 42390 ,
5, "2020-10-28 09:41:05", "Warnings" , "{QRA3.-[ENTR]}{Q7.19-[HNGZ027]}", 42710 ,
6, "2020-10-28 09:41:05", "Warnings" , "{Q9.06-[HI0220]}{Q9.01-[HI0100]}{Q9.04-[HNI0200]}{Q9.05-[HNI0210]}", 42710 ,
7, "2020-10-28 09:41:05", "Warnings" , "{Q9.16-[HI0600]}", 42710,
8, "2020-10-28 09:41:05", "Warnings" , "{Q9.24-[HIZ040_1]}", 42710,
9, "2020-10-28 09:41:05", "Warnings" , "{QRA3.-[CONF]}", 42480 ,
10, "2020-10-28 09:41:05", "Warnings" , "{Q2.01-[HB0100]}", 42480
)
# Create one aditional column
base_var <- base_var %>%
mutate(
err_history = split(., as.factor(.$ErrID)))
# Operational Phase
# Some values change
base_var[1:2, 'Status'] <- "Solved"
# Working: Updating History for Row 1
base_var$err_history[[1]] <- bind_rows(base_var[1, 1:5], base_var$err_history[[1]])
# Multiple row changes!! Is there a Better way that using 'for'?
for (i in 1:2) {
base_var$err_history[[i]] <- bind_rows(base_var[i, 1:5], base_var$err_history[[i]])
}