【问题标题】:Convert NaN to NA in a tibble在小标题中将 NaN 转换为 NA
【发布时间】:2018-07-01 12:33:53
【问题描述】:

我使用readr 包导入了一个.csv(我们称之为x),它产生了一个tibble。

编辑:由于 readr 生成的 actual tibble 与下面发布的 problems(x)-tibble 之间存在混淆,因此这里是生成的 actual tibble 的开头问题

> x
# A tibble: 46,080 x 18
      x_1   x_2   x_3   x_4   x_5   x_6    x_7     x_8    x_9     x_10      x_11        x_12        x_13      x_14  x_15
    <int> <int> <int> <int> <int> <dbl>  <dbl>   <dbl>  <dbl>    <dbl>     <dbl>       <dbl>       <dbl>     <dbl> <int>
 1     1     1     1     1    29  84.4   72.5  10.1     48.5     35.3      34.2        293.        117.      24.5    20
 2     1     1     1     2   120 214.   142.   -0.488   55.8     42.1      36.3        589.        124.     257.     84
 3     1     1     1     3    28 258.    42.3   2.09    43.7     29.2      32.1        352.        117.      72.2    19
 4     1     1     1     4    39 623.   249.   12.1     95.7     75.7      58.6        998.        176.     243.     14
 5     1     1     1     5   222 320.   244.   -2.10    70.7     51.4      48.4       1232.        242.     711.    111
 6     1     1     1     6    33 485.   142.   12.3     61.8     51.9      34.6        764.        117.     160.     24
 7     1     1     1     7    32 884.   458.   11.0    110.      88.1      64.5       1525.        237.     283.      5
 8     1     1     1     8    58 695.   187.  -12.7     64.6     50.5      41.7       1090.        175.     403.     37
 9     1     1     2     1    46  58.0   65.3   5.10    49.4     35.2      34.7        234.        117.      26.7    18
10     1     1     2     2   136 217.   191.   -0.431   60.5     43.2      42.2        706.        185.     295.     72
# ... with 46,070 more rows, and 3 more variables: x_16 <dbl>, x_17 <dbl>, x_18 <dbl>

我尝试了read_csvna = 属性的各种组合,以避免错误地读取数据,但是,我没有使它适用于我的情况: 在使用 readr 包时,我收到一条关于某些列中的问题的消息,所以我使用&gt;problems(x) 来了解发生了什么。这是输出:

> problems(x)
# A tibble: 264 x 5
     row col   expected   actual file                              
   <int> <chr> <chr>      <chr>  <chr>                             
 1  1992 x_5  an integer NaN    'raw-data/x.csv'
 2  1992 x_15 an integer NaN    'raw-data/x.csv'
 3  2320 x_5  an integer NaN    'raw-data/x.csv'
 4  2320 x_15 an integer NaN    'raw-data/x.csv'
 5  2581 x_5  an integer NaN    'raw-data/x.csv'
 6  2581 x_15 an integer NaN    'raw-data/x.csv'
 7  2582 x_5  an integer NaN    'raw-data/x.csv'
 8  2582 x_15 an integer NaN    'raw-data/x.csv'
 9  2583 x_5  an integer NaN    'raw-data/x.csv'
10  2583 x_15 an integer NaN    'raw-data/x.csv'
# ... with 254 more rows

我确实明白,显然在几列和几行中,.csv 读取失败,这导致在需要整数的字段中出现 NaN。

我尝试使用 is.nan 方法将这些 NaN 转换为“真正的”NA,但这失败了,因为该方法似乎不支持整个 tibbles。

> x[is.nan(x)] <- NA #convert NaN to NA
    Error in is.nan(x): default method not implemented for type 'list'

我也尝试使用 naniar 包中的 replace_with_na_all 方法,但这也失败了

> replace_with_na_all(data = x, condition = ~.x == NaN)
    Error in .x[sel] <- map(.x[sel], .f, ...) : NAs are not allowed in subscripted assignments

因此,我正在寻找一种方法来一次性转换所有列和所有行中的 所有 NaN,或者避免在 read_csv 期间一起创建 NaN。

【问题讨论】:

  • x$actual[is.nan(x$actual)] &lt;- NA
  • x[] &lt;- lapply(x, function(a) ifelse(is.nan(a), NA_real_, a))
  • @r2evans 您能否详细说明代码的作用以及我是否需要在此处发布两行代码。或者您可以将其发布为答案吗?
  • 您的问题涉及数据类型——您的 x 列是一个字符,因此 is.nan("NaN") 返回 false,因为它只是读取字符串,而不是实际的 NaN 值。如果您发布dput 的输出会更有帮助,因为这会让帮助您的人更清楚这个问题
  • @camille 我的readr 输出(您自动获得的那个)实际上指定有问题的列(x_1x_2)实际上被识别为 col_integer(),因此我是不确定“读取字符串”是否适用于此。关于dput:你的意思是我应该发布 dput(x) 的输出吗?这会返回一个很大的输出,可以在此处发布。

标签: r nan tibble


【解决方案1】:

虽然这只是我自己问题的部分答案(它没有告诉您如何将NaN 转换为NA),但我想指出一个可能的解决方案,以防出现问题同根。

我想用readr 导入的.csv 由Matlab 写出,并在Matlab 中值为NaN 的单元格中包含字符串NaN。因此,不是 R 有识别数字的问题,而是 NaN 包含为字符串的问题。

read_csv 中使用na = "NaN" 属性显然解决了这个问题。

【讨论】:

    猜你喜欢
    • 2019-02-28
    • 2018-01-04
    • 2014-09-20
    • 2023-01-20
    • 2021-12-24
    • 2012-03-25
    • 2020-01-07
    • 2011-11-24
    • 2020-02-11
    相关资源
    最近更新 更多