串联R中的两个向量 [英] Concatenating two vectors in R

查看:97
本文介绍了串联R中的两个向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在R中一个接一个地连接两个向量。我编写了以下代码来做到这一点:

I want to concatenate two vectors one after the other in R. I have written the following code to do it:

> a = head(tracks_listened_train)
> b = head(tracks_listened_test)
> a
[1] cc1a46ee0446538ecf6b65db01c30cd8 19acf9a5cbed34743ce0ee42ef3cae3e
[3] 9e7fdbf2045c9f814f6c0bed5da9bed7 3441b1031267fbb6009221bf47f9c5e8
[5] 206c8b79bd02beeea200879afc414879 1a7a95e3845a6815060628e847d14362
18585 Levels: 0001a423baf29add84af6ec58aeb5b90 ...
> b
[1] 7251a7694b79aa9a39f9a1a5f5c8a253 2f362377ef0e7bca112233fdda22a79c
[3] c1196625b1b733b62c43935334e1d190 58e41e462af4185b08231a41453c3faf
[5] 1cc2517fa9c037e02a14ce0950a28f67
10186 Levels: 0001a423baf29add84af6ec58aeb5b90 ...
> res = c(a,b)
> res
[1] 14898  1898 11556  3859  2408  1950  4473  1865  7674  3488  1130

我得到了合成向量的意外结果。可能是什么问题?

However, I get the unexpected result of the resultant vector. What could the problem be?

推荐答案

我们需要将 factor 类转换为字符

c(as.character(a), as.character(b))






我们获取数字而不是 character 基于 factor 的存储模式,即整数。因此,当我们进行串联时,它会强制为整数模式


The reason we get numbers instead of the character is based on the storage mode of factor i.e. an integer. So when we do the concatenation, it coerces to the integer mode

这篇关于串联R中的两个向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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