本文介绍了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",而不是数字

but I want 'setosa', etc in column y instead of a number

我该怎么做?

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

I want to combine the 2 Vectors because I want to filter afterwards with

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

还是没有cbind的另一个机会?

or is ther another oportunity to do that without cbind?

推荐答案

对于将vectorscbind结合使用,结果将是matrix,它只能保存一种类型的数据.因此,种类"因子被强制为其基础数字值.

For vectors being combined with cbind, the result would be a matrix, which can only hold one type of data. Thus, the "Species" factor gets coerced to its underlying numeric value.

如果需要您的列具有不同的数据类型,请尝试使用cbind.data.frame(或只是data.frame).

Try cbind.data.frame instead (or just data.frame) if you need your columns to have different data types.

> 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

这篇关于cbind用数字替换String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 13:48