I have the following two data frames in a list. For each data frame, I would like to calculate the difference between the number of observations of a group (indicated by a “type”) that contains the maximum value (“value”) and the number of observations of another group.
So, for df1 this will be 3 - 6 = -3, since type B contains a maximum value of 7, and for type B and 6 observations there are 3 observations for type A.
value <- c(1, 2, 3, 4, 5, 6, 1, 2, 7)
type <- c("A", "A", "A", "A", "A", "A", "B", "B", "B")
df1 <- data.frame(value, type)
value <- c(1, 2, 3, 4, 6, 1, 2)
type <- c("A", "A", "A", "A", "A", "B", "B")
df2 <- data.frame(value, type)
mylist <- list(df1, df2)
I think this would be the next line combined with length(unique())and max(), but I can’t figure it out.
calculation <- lapply(mylist, function (x)
{x$