[英]Histogram from nested list in R
我有一個類似於這個的嵌套列表
df <- data.frame(width = c(1:5),height = c(1:5),depth = c(1:5))
list <- list(df,df*2,df*5,df*1.3)
我想在每個列表的最終高度的基礎 R 或 ggplot 中制作直方圖。 我努力了
hist(tail(list[[1:4]]$height,1))
這產生
Error in list[[1:4]] : recursive indexing failed at level 3
也
for (i in 1:4){
hist(tail(list[[i]]$height,1))
}
它生成單個觀察的 4 個單獨的直方圖。 我試圖避免簡單地在hist()
列出每個觀察結果,例如
hist(tail(list[[1]]$height,1),tail(list[[2]]$height,1),tail(list[[3]]$height,1))
因為我的實際數據有超過 4 個子列表。 我怎樣才能優雅地實現這一目標?
如果我們想提取 'height' 的最后一個元素,請使用sapply
list
,提取( $
)列 'height',獲取tail
值( sapply
- 此處返回一個vector
),然后應用hist
hist( sapply(list, function(x) tail(x$height, 1)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.