【问题标题】:pot histograms and had error "missing value where TRUE/FALSE needed"罐直方图并出现错误“需要 TRUE/FALSE 的缺失值”
【发布时间】:2021-06-02 18:01:58
【问题描述】:

更新:

原来这是由不同类别的变量引起的。

非常感谢@r2evans,他在读取数据时通过将 interger64 转换为数字解决了这个问题。他的方法是有效的,但值得进一步研究的是他解决问题的逻辑。

出于保密原因,我删除了数据。

下面是上一题

我在我的数据表中绘制了所有数字克洛蒙的直方图。

head(dt) %>%
  keep(is.numeric) %>% 
  gather() %>% na.omit() %>% 
  ggplot(aes(value)) +
  facet_wrap(~ key, scales = "free") +
  geom_histogram()

由于数据表太大,我选择了head()。

然后我遇到了这个错误:

if (length(unique(intervals)) > 1 & any(diff(scale(intervals))

那我让

eg <- head(dt)
write.csv2(head(dt), "eg.csv")

并保存在 github 上,例如 here

然后

eg <- fread("https://raw.githubusercontent.com/Deborah-Jia/Complete_Analysis_da2/main/eg.csv")

eg %>%
  keep(is.numeric) %>% 
  gather() %>% na.omit() %>% 
  ggplot(aes(value)) +
  facet_wrap(~ key, scales = "free") +
  geom_histogram()

我得到了正确的直方图!

当我保存数据并再次读取时发生了什么?或者有没有办法修复dt?

PS:dt 也是通过保存 csv 和从 fread 读取创建的。 当我使用

eg <- head(dt, 10000)

并保存在github,再读一遍。发生了同样的错误。

是不是因为我的 dt 太长(300 万行)并且有一些错误的行?

【问题讨论】:

  • 您的第二个(工作)代码块包含na.omit(),而您的第一个(失败)代码不包含。真正的比较将使调用之间的代码标准化。 (在不知道write.csv2之前的数据的情况下,不知道我们能确定问题是什么。)
  • 谢谢!我意识到我有一个错字。 tbh 两个块都有 na.omit.
  • 由于它适用于您和我们的 githubusercontent 数据,因此向我们提供 pre-write.csv2 数据可能会有所帮助。你能粘贴dput(head(dt))的输出吗?
  • 谢谢! dt 的较大部分在这里:raw.githubusercontent.com/Deborah-Jia/Complete_Analysis_da2/… 使用相同的绘图代码,你能看看这个错误吗?

标签: r histogram


【解决方案1】:

问题 症状是您的两个字段出现 出现强> 不变。下载完整数据后dt

dt <- fread("https://raw.githubusercontent.com/Deborah-Jia/Complete_Analysis_da2/main/eg1.csv")
dt %>%
  keep(is.numeric) %>% 
  gather() %>%
  na.omit() %>%
  group_by(key) %>%
  summarize(v = var(value))
# Warning: attributes are not identical across measure variables;
# they will be dropped
# # A tibble: 9 x 2
#   key                         v
#   <chr>                   <dbl>
# 1 area_size_high        1.00e18
# 2 area_size_low         3.64e10
# 3 lot_size_high         8.76e17
# 4 lot_size_low          5.60e 5
# 5 price_huf_high        0.         ### problem!
# 6 price_huf_low         0.     
# 7 total_room_count_high 3.23e17
# 8 total_room_count_low  1.46e 0
# 9 V1                    8.33e 6

(当数据不变时,许多图倾向于内爆。)

不过,这很令人困惑,因为head(dt) 肯定会显示不同的值(右侧):

          V1         ds                            search_id property_type property_subtype price_huf_low price_huf_high
       <int>     <IDat>                               <char>        <char>           <char>         <i64>          <i64>
    1:     1 2021-02-15 ad2be212-0c25-4e3a-aabf-be089053beba         house             <NA>      45000000       69000000
    2:     2 2021-02-15 ab72ba19-d00f-49e2-8d0d-c6836f030758     apartment             <NA>             0       48000000
    3:     3 2021-02-06 24bbb050-2ecb-4078-a8dc-65e968f72f43     apartment             <NA>     150000000      200000000
    4:     4 2021-02-06 f7d87e6e-0f24-4d9e-ae82-2a448d6290bf     apartment             <NA>       2000000       29000000
    5:     5 2021-02-14 71ea3cc4-5326-4bbe-a2ff-20dbae0d9aa8     apartment             <NA>     200000000      400000000

(截断)。

但是,看到那里的关键是i64,注意这些是 64 位整数。

sapply(dt, function(z) class(z)[1])
#                    V1                    ds             search_id         property_type      property_subtype 
#             "integer"               "IDate"           "character"           "character"           "character" 
#         price_huf_low        price_huf_high         area_size_low        area_size_high          lot_size_low 
#           "integer64"           "integer64"             "integer"             "integer"             "integer" 
#         lot_size_high  total_room_count_low total_room_count_high              district 
#             "integer"             "integer"             "integer"           "character" 

您可以通过以下两种方式之一解决此问题:

  1. 在阅读时修复它(推荐):

    dt <- fread("https://raw.githubusercontent.com/Deborah-Jia/Complete_Analysis_da2/main/eg1.csv",
                integer64 = "numeric")
    
  2. 用您环境中的数据修复它:

    ### data.table (since you used `fread`)
    dt[, c("price_huf_low", "price_huf_high") := lapply(.SD, as.numeric),
       .SDcols = c("price_huf_low", "price_huf_high")]
    
    ### or dplyr
    dt %>%
      mutate(across(starts_with("price"), as.numeric)) %>% # ... rest of your pipe
    ### if more than 'price_*' columns:
    dt %>%
      mutate(across(where(~ inherits(., "integer64")), as.numeric)) %>% # ...
    

无论哪种方式,一旦将这两列转换为numeric,就可以使用您的原始代码绘制它们:

dt %>%
  keep(is.numeric) %>% 
  gather() %>% na.omit() %>% 
  ggplot(aes(value)) +
  facet_wrap(~ key, scales = "free") +
  geom_histogram()

【讨论】:

  • 谢谢亲爱的 r2evans!我从没想过这会是一个班级问题!我试过你的方法,效果很好!价格有一些极端值,我想在绘制直方图并使用描述/摘要后删除它们。结果是这个int64 prob,我什至不能使用Hmisc::describe()!您的解决方案保存了我的项目。我不知道该如何表达我的感激之情。
猜你喜欢
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 2019-03-07
  • 2019-02-25
  • 2013-02-24
  • 2013-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多