I have a data frame which can be represented by this sample:
structure(list(Created = structure(c(1723114609.889, 1723120797.26,
1723129821.436), tzone = "UTC", class = c("POSIXct", "POSIXt"
)), `Root Cause` = list(structure(list(type = "text", text = "Sample Text"), class = "data.frame", row.names = 1L),
NULL, NULL)), class = c("rowwise_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L), groups = structure(list(.rows = structure(list(
1L, 2L, 3L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame")))
How can I mutate the Root Cause column so that entries which contain NULL are replaced with “tbc” and entries which contain data are replaced with the data in the text field, i.e. the Root Cause for the first row will read “Sample Text”?
I can achieve it with a for
loop with
for(i in 1:nrow(issues_df)) {
if(ncol(as.data.frame(issues_df$`Root Cause`[i])) == 0) {
issues_df$`Root Cause`[i] <- "TBC"
} else {
issues_df$`Root Cause`[i] <- as.data.frame(as.data.frame(issues_df$`Root Cause`[i])$content)$text
}
}
But I feel like doing it within a mutate would be a more efficient and idiomatic approach.