如何在强制引入NAs时避免警告 [英] How to avoid warning when introducing NAs by coercion

查看:455
本文介绍了如何在强制引入NAs时避免警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常喜欢代码R,所以我不会得到警告,但我不知道如何避免在使用 as.numeric 转换时发出警告一个字符向量。

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.

例如:

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

我警告,因为它强行引入NAs。我想通过强制介绍的NA - 是否有一种方法来告诉它是的,这是我想做的。

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?

还是我应该使用不同的函数来完成此任务?

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

推荐答案

使用 suppressWarnings()

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

这会禁止警告。

这篇关于如何在强制引入NAs时避免警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆