I want to compare a certain value in a list of lists in R. This is a shortened example.
list1=rbind(element, data.frame(name=c("city", "town", "village"),size=c(5,3,8),height=c(13, 20,19)))
list2=rbind(element, data.frame(name=c("city", "town", "village"),size=c(4,6,3), height=c(11, 22,14)))
list3=rbind(element, data.frame(name=c("city", "town", "village"),size=c(7,5,9), height=c(23, 16,11)))
list=list(list1, list2, list3)
I want to compare all lists according to name= "village" and find the minimum, median and maximum value for "size". Additionally I want the number of the lists with the minimum, median and maximum value. Actually there are many lists. I tried the folowing. It works, but probably there is an easier way. Especially, to find the number of the list.
dt_list <- map(list, as.data.table)
dt <- rbindlist(dt_list, fill = TRUE, idcol = T)
village_list=dt%>% filter(name=="village")
village_size<- village_list[,3]
summary (village_size)
CodePudding user response:
Bind the data frames into a single one and summarise to get the stats you're looking for:
library(dplyr)
list %>%
bind_rows(.id="id") %>%
filter(name == 'village') %>%
summarise(min(size), max(size), median(size), id[which.min(size)], id[which.max(size)], id[which.min(abs(size - median(size)))])