You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言密度图绘制前剔除缺失值失败问题求助

如何在绘制密度图前正确剔除缺失值?

我尝试在绘制密度图前剔除缺失值,但多次尝试均未成功,请求帮助:

失败的尝试

  1. 执行以下代码报错:
d <- density(Data$item2) %>%
  na.omit() 

报错信息:Error in density.default(Data$item2) : 'x' contains missing values

  1. 以下写法同样无效:
d <- Data %>% na.omit() %>% 
  density(Data$item2)
  1. 我还尝试在读取数据时直接剔除缺失值:
Data <- read_excel("C:/location/Data.xlsx") %>% 
  na.omit()

但问题仍未解决。

我的数据结构

structure(list(item1 = c(5, 5, 5, 5, 4, 4, 2, 1, 3, 
4, 4, 3, 2, 5, 2, 4, 4, 3, 6, 5, 3, 2, 5, 3, 3, 1, 3, 5, 1, 3, 
2, 6, 3, 5, 4, 4, 3, 5, 6, 3, 2, 6, 6, 5, 2, 2, 2, 3, 3, 3), 
    item2 = c(5, 4, 5, 1, 2, 2, 3, 2, 2, 2, 2, 3, 2, 
    5, 1, 4, 4, 3, 3, 5, 3, 2, 4, 4, 3, 4, 4, 3, 7, NA, 2, 4, 
    2, 4, 2, 3, 5, 3, 5, 3, 2, 6, 6, 7, 2, 3, 2, 3, 1, 4)), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))

请问如何正确剔除缺失值后绘制密度图?


内容的提问来源于stack exchange,提问作者Espejito

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 20:43:52