I have a dataframe containing three columns, two of which can contain either numeric values or lists. I would like to add additional columns containing the min / max values of each of these two columns. For example, my data frame may look like;
df <- structure(list(ID = c(1L, 2L, 3L), A = structure(list(
5, c(0.5, 0.6), 2), names = c("", "", "")), B = structure(list(
c(0.2, 0.3), 6, c(0.1, 0.1)), names = c("", "", ""))), row.names = c(NA,
3L), class = "data.frame")
I would like to mutate this to add the columns;
ID | A | B | min_A | max_A | min_B | max_B |
---|---|---|---|---|---|---|
1 | 5 | 0.2, 0.3 | 5 | 5 | 0.2 | 0.3 |
2 | 0.5, 0.6 | 6 | 0.5 | 0.6 | 6 | 6 |
3 | 2 | 0.1, 0.1 | 2 | 2 | 0.1 | 0.1 |
I have tried mutate(min_A = min(unlist(A)))
, but this seems to take the minimum value of the entire column of A rather than just the list on any given row. mutate(min_A = min(A))
errors out because list is an invalid argument type for the min
command. So how might I go about adding the data I'm after?