强制引入NAs时如何避免警告

时间:2021-03-31 06:54:19

I generally prefer to code R so that I don't get warnings, but I don't know how to avoid getting a warning when using as.numeric to convert a character vector.

我通常喜欢编写R代码,这样就不会收到警告,但是我不知道如何在使用as时避免收到警告。数字转换字符向量。

For example:

例如:

x <- as.numeric(c("1", "2", "X"))

Will give me a warning because it introduced NAs by coercion. I want NAs introduced by coercion - is there a way to tell it "yes this is what I want to do". Or should I just live with the warning?

我会给我一个警告,因为它通过强制引入NAs。我想用强制的方式引入NAs——有一种方法可以告诉它“是的,这就是我想要做的”。还是我应该接受警告?

Or should I be using a different function for this task?

或者我应该为这个任务使用不同的函数?

4 个解决方案

#1


110  

Use suppressWarnings():

使用suppressWarnings():

suppressWarnings(as.numeric(c("1", "2", "X")))
[1]  1  2 NA

This suppresses warnings.

这抑制了警告。

#2


27  

suppressWarnings() has already been mentioned. An alternative is to manually convert the problematic characters to NA first. For your particular problem, taRifx::destring does just that. This way if you get some other, unexpected warning out of your function, it won't be suppressed.

已经提到了suppresswarning()。另一种方法是先手工将有问题的字符转换为NA。对于您的特殊问题,taRifx::destring就是这样做的。这样,如果你从你的函数中得到一些意外的警告,它就不会被抑制。

> library(taRifx)
> x <- as.numeric(c("1", "2", "X"))
Warning message:
NAs introduced by coercion 
> y <- destring(c("1", "2", "X"))
> y
[1]  1  2 NA
> x
[1]  1  2 NA

#3


13  

In general suppressing warnings is not the best solution as you may want to be warned when some unexpected input will be provided.
Solution below is wrapper for maintaining just NA during data type conversion. Doesn't require any package.

一般来说,抑制警告不是最好的解决方案,因为您可能希望在提供一些意外输入时得到警告。下面的解决方案是在数据类型转换期间仅维护NA的包装器。不需要任何包。

as.num = function(x, na.strings = "NA") {
    stopifnot(is.character(x))
    na = x %in% na.strings
    x[na] = 0
    x = as.numeric(x)
    x[na] = NA_real_
    x
}
as.num(c("1", "2", "X"), na.strings="X")
#[1]  1  2 NA

#4


0  

You can use the library stringr.

您可以使用库stringr。

library(tidyverse) #For piping 
library(stringr) #Note: it's part of "tidyverse"

  c("1", "2", "X") %>%
    stringr::str_extract_all("\\(?[0-9,.]+\\)?") %>% 
    as.numeric()

#1


110  

Use suppressWarnings():

使用suppressWarnings():

suppressWarnings(as.numeric(c("1", "2", "X")))
[1]  1  2 NA

This suppresses warnings.

这抑制了警告。

#2


27  

suppressWarnings() has already been mentioned. An alternative is to manually convert the problematic characters to NA first. For your particular problem, taRifx::destring does just that. This way if you get some other, unexpected warning out of your function, it won't be suppressed.

已经提到了suppresswarning()。另一种方法是先手工将有问题的字符转换为NA。对于您的特殊问题,taRifx::destring就是这样做的。这样,如果你从你的函数中得到一些意外的警告,它就不会被抑制。

> library(taRifx)
> x <- as.numeric(c("1", "2", "X"))
Warning message:
NAs introduced by coercion 
> y <- destring(c("1", "2", "X"))
> y
[1]  1  2 NA
> x
[1]  1  2 NA

#3


13  

In general suppressing warnings is not the best solution as you may want to be warned when some unexpected input will be provided.
Solution below is wrapper for maintaining just NA during data type conversion. Doesn't require any package.

一般来说,抑制警告不是最好的解决方案,因为您可能希望在提供一些意外输入时得到警告。下面的解决方案是在数据类型转换期间仅维护NA的包装器。不需要任何包。

as.num = function(x, na.strings = "NA") {
    stopifnot(is.character(x))
    na = x %in% na.strings
    x[na] = 0
    x = as.numeric(x)
    x[na] = NA_real_
    x
}
as.num(c("1", "2", "X"), na.strings="X")
#[1]  1  2 NA

#4


0  

You can use the library stringr.

您可以使用库stringr。

library(tidyverse) #For piping 
library(stringr) #Note: it's part of "tidyverse"

  c("1", "2", "X") %>%
    stringr::str_extract_all("\\(?[0-9,.]+\\)?") %>% 
    as.numeric()