简体   繁体   English

更改 R 中变量的类别:警告消息

[英]Changing the category of a variable in R: Warning message

I'm trying to change the participants' Age variable (in my dataset) that's showing as character (rather than numeric) using the following code..我正在尝试使用以下代码更改显示为字符(而不是数字)的参与者的年龄变量(在我的数据集中)。

bwdata6 <- bwdata6 %>% mutate(Age <- as.numeric(Age))

I get the following warning message when I run the code...我在运行代码时收到以下警告消息...

Warning messages: 1: Problem with mutate() input ..1. i NAs introduced by coercion
Input ..1 is Age <- as.numeric(Age). 2: In mask$eval_all_mutate(dots[[i]]) : 
  NAs introduced by coercion

Any ideas how to resolve this?任何想法如何解决这个问题?

Without a warning you may use gsub .您可以在没有warning的情况下使用gsub

d$x.num <- as.numeric(gsub("\\D", NA, d$x))

to identify those values that become NA accordingly, :识别那些相应地成为NA的值,:

grep("\\D", d$x)
# [1] 2 4 6

d
#   x x.num
# 1 1     1
# 2 A    NA
# 3 2     2
# 4 B    NA
# 5 3     3
# 6 C    NA

Data:数据:

d <- data.frame(x=c(1, "A", 2, "B", 3, "C"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM