无法在R中将字符转换为数字 [英] Can't convert character to numeric in R

查看:96
本文介绍了无法在R中将字符转换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将值从字符转换为数字,但是R引入了NA。我已经检查了一个有问题的值:

I'm trying to convert values from character to numeric, but R introduces NAs. I have examined one of the problematic values:

> x
[1] "11 914 711.5"
> length(x)
[1] 1
> typeof(x)
[1] "character"
> as.numeric(x)
[1] NA
Warning message:
NAs introduced by coercion 

有人知道如何解决此问题吗?

Does anybody have any idea how to fix this?

推荐答案

您可以先拆分字符串,然后将其转换为数字:

You could first split your string and then convert them to numeric:

as.numeric(unlist(strsplit(x, ' ')))
[1]  11.0 914.0 711.5

这篇关于无法在R中将字符转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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