cbind用数字替换String?

用户名
x = iris$Sepal.Width;
y = iris$Species;

m = cbind(x,y);

m的输出为:

        x  y
  [1,] 3.5 1
  [2,] 3.0 1
  [3,] 3.2 1
  [4,] 3.1 1
  [5,] 3.6 1
  [6,] 3.9 1

但我想在y列中使用“ setosa”等,而不是数字

我怎样才能做到这一点?

我想合并2个向量,因为之后我想用

m[m[,"y"]=="virginica",]

还是在没有限制的情况下这样做的另一个机会?

A5C1D2H2I1M1N2O1R2T1

对于vectors被联合cbind,其结果将是一个matrix,它只能容纳一个类型的数据。因此,“种类”因子被强制为其基础数字值。

如果需要您的列具有不同的数据类型,请尝试cbind.data.frame替代(或仅尝试data.frame)。

> head(data.frame(x, y))
    x      y
1 3.5 setosa
2 3.0 setosa
3 3.2 setosa
4 3.1 setosa
5 3.6 setosa
6 3.9 setosa
> head(cbind.data.frame(x, y))
    x      y
1 3.5 setosa
2 3.0 setosa
3 3.2 setosa
4 3.1 setosa
5 3.6 setosa
6 3.9 setosa

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章