R如何自动将字符输入强制转换为数字?

编程入门 行业动态 更新时间:2024-10-28 20:30:13
本文介绍了R如何自动将字符输入强制转换为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在 randomForest 包中训练我的数据的随机森林模型。有些变量属于字符类。我很确定 randomForest 仅将因子和数字类作为输入。因此,我认为R会自动将字符强制转换为数字。

I am training a random forest model in the randomForest package for my data. Some variables are in the class of character. I am pretty sure that randomForest will only take factor and numeric classes as input. So I think R automatically coerces the character into numeric.

为了让我知道这会如何影响我的建模结果,有人知道R如何将字符自动强制转换为数字类(例如算法/规则)?还是我可以查看的任何源代码?

In order for me to know how this may affect my modelling result, does anyone know how R automatically coerces the character into numeric class (like an algorithm/rule)? Or any source code I can look at?

我正在使用R版本4.0.1。

I am using R version 4.0.1.

预先感谢。

更新: 我使用了

An update: I checked using

getTree(mod,1,labelVar=TRUE)

我可以看到,如果将这些字符变量转换为因子,则分割点将变为在输出中为整数(表示它是类别变量(请参阅: www.rdocumentation/packages/randomForest/versions/4.6-14/topics/getTree ))。但是,如果不转换为因子,则分裂点将被分解。在输出中不是整数。

And I can see that if those character variables are converted to factors, then the "split point" in the output is an integer (which means it is a categorical variable (see: www.rdocumentation/packages/randomForest/versions/4.6-14/topics/getTree)). But if not converted to factors, then the "split point" in the output is not integer.

所以我想R是将那些字符变量的值强制转换为数字值吗?

So I guess is that R coerces the values of those character variables into numeric values? But how?

推荐答案

目前不确定R中的随机森林,但我确信它仅需要个因素 s。如果确实也需要个字符,它将把它们转换为因数,而不是数字。

Not sure right now regarding the random forests in R, but I am kind of convinced, that it only takes factors. If it does take characters as well, it will convert them to factor, not to numeric.

并且没有明确的转换从字符到R中的数字。

And there is no clear conversion from character to numeric in R.

更多推荐

R如何自动将字符输入强制转换为数字?

本文发布于:2023-11-23 14:58:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!