我如何追加一个int载体的一个因素? [英] How do I append an int vector to a factor?

查看:138
本文介绍了我如何追加一个int载体的一个因素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵称为矩阵,看起来像这样:

 负责convexhull_xCoor convexhull_yCoor ID强度
3 3336.43 667.62 f_7936733956214261295 475891
3 3339.73 667.6 f_7936733956214261295 475891

我得到两个向量,身份证和强度:

  idVector =矩阵[4]
intensityVector =矩阵[5]

我想这两个向量加在一起使用附加:

  bigVector =追加(idVector,intensityVector)

不过,我这样做时,我得到这个结果:

  [1] 4 3 2 1 475891 5490000 1860000 1100000

研究取得了CLASS =因素出idVector的,当我追加intVector的它,它是不是追加到标签。我怎么可以追加一个int载体的一个因素?

下面是可重复code,我只有麻烦给dput(头(矩阵,4)),因为它给了所有的ID这是相当多的,我给了dput(头(矩阵,4) ),而不是载体。

 向量1 =结构(C(4L,3L,2L,1L),.Label = C(f_15177294341548527346,f_18178836531573487427,f_2444900193131259878,f_7936733956214261295),类=因子)
vector2 = C(475891,5490000,1860000,1100000)
bigVector =追加(向量1,vector2)
向量1
vector2
bigVector


解决方案

您不能混淆因素和放大器;这样的数字在向量 - 你必须使用一个数据帧

  bigdf<  -  data.frame(ID = idVector,强度= intensityVector)

然后看看 bigdf (并且你可以通过 bigdf的$ id 等访问列)。

另外,如果 idVector 的元素是唯一的,您可以添加 idVector 作为名称的属性你的 intensityVector

 名称(intensityVector)LT;  -  idVector

然而,ID不再是一个因素,但是你可以通过一个特定的 ID参考值强度 intensityVector ['f_7936733956214261295']

数据帧的方法是几乎总是更好,因为它是非常非常适合进行统计分析。

I have a matrix called matrix that looks like this:

charge  convexhull_xCoor    convexhull_yCoor    id              intensity
3       3336.43             667.62       f_7936733956214261295  475891
3       3339.73             667.6        f_7936733956214261295  475891

I get two vectors, id and intensity:

idVector = matrix[4]
intensityVector = matrix[5]

I want to add these two vectors together using append:

bigVector = append(idVector, intensityVector)

However, when I do this I get this as a result:

[1]       4       3       2       1  475891 5490000 1860000 1100000

R made a class = factor out of the idVector and when I appends the intVector to it, it is not appending it to the labels. How can I append an int vector to a factor?

Below is the reproducible code, I only have trouble giving the dput(head(matrix,4)) because it gives all the id's which are quite a lot, I gave the dput(head(matrix,4)) of the vectors instead.

vector1 = structure(c(4L, 3L, 2L, 1L), .Label = c("f_15177294341548527346", "f_18178836531573487427", "f_2444900193131259878", "f_7936733956214261295"), class = factor")
vector2 = c(475891, 5490000, 1860000, 1100000)
bigVector = append(vector1, vector2)
vector1
vector2
bigVector

解决方案

You can't mix factors & numbers like that in a vector - you have to use a data frame.

bigdf <- data.frame( id=idVector, intensity=intensityVector )

Then have a look at bigdf (and you can access columns via bigdf$id, etc).

Alternatively, if the elements of idVector are unique, you could add idVector as the names attribute of your intensityVector:

names(intensityVector) <- idVector

However the id is no longer a factor, but you can refer to values in intensity by a particular id as in intensityVector['f_7936733956214261295'].

The data frame approach is almost always better because it's very well-suited to statistical analysis.

这篇关于我如何追加一个int载体的一个因素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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