从两个向量创建新的ID(双向) [英] Creating new IDs (two-way) from two vectors

查看:106
本文介绍了从两个向量创建新的ID(双向)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据帧中有两个向量,这些向量由因子组成(每个因子有10000个唯一因子)。



这是我的数据的简化示例:

  tg< -data.frame(A = sample(letters [1:5],30,replace = TRUE),
B = sample(letters [1:5],30,replace = TRUE))
AB
1 bb
2 ab
3 ee
4 cb
5 ee
6 ab
7 da
8 da
9 db
10 ae
11 ae
。 。 。

我想(逐行)组合两列中的元素并创建一个新的ID。 / p>

一种方法是简单地将两列粘贴在一起:

  paste0 (tg $ A,tg $ B)
[1] bb ab ee cb ee ab da da db ae
ae bc ae bb ec ba dd de cd eb cd
db dc ab ae cc dc ce eb ea

但是我想制作将 ab和 ba视为相同的新ID,该怎么办?



编辑:对不起令人困惑的标题,我找不到更好的方法来解释该问题(因此无法找到任何解决方案-如果已经有了解决方案)。

解决方案

在ID内排序字母可以解决该问题(在这个问题):

  strSort和lt;-function(x)
sapply(lapply(strsplit(x,NULL),sort),paste,crash =)
unique(sapply(new_ids,strSort))
[ 1] be bd bc ae aa ab ad cd ac dd ee ce cc de


I have two vectors in a data frame consisting of factors (~10000 unique factors in each).

Here's a simplified example of how my data look like:

tg<-data.frame(A=sample(letters[1:5],30,replace=TRUE),
               B=sample(letters[1:5],30,replace=TRUE))
   A B
1  b b
2  a b
3  e e
4  c b
5  e e
6  a b
7  d a
8  d a
9  d b
10 a e
11 a e
. . .

I want to combine (row-wise) the elements in both columns and create a new ID.

One way would be to simply paste both columns together:

paste0(tg$A,tg$B)
[1] "bb" "ab" "ee" "cb" "ee" "ab" "da" "da" "db" "ae"
"ae" "bc" "ae" "bb" "ec" "ba" "dd" "de" "cd" "eb" "cd"
"db" "dc" "ab" "ae" "cc" "dc" "ce" "eb" "ea"

But I would like to make new IDs that regard "ab" and "ba" as the same, how can I do that?

Edit: sorry for the confusing title, I couldn't find any better way to explain the problem (hence couldn't find any solution for it - if there's one here already).

解决方案

Sorting letters within your ID's will resolve the problem (following this question):

strSort <- function(x)
     sapply(lapply(strsplit(x, NULL), sort), paste, collapse="")
unique(sapply(new_ids, strSort))
 [1] "be" "bd" "bc" "ae" "aa" "ab" "ad" "cd" "ac" "dd" "ee" "ce" "cc" "de"

这篇关于从两个向量创建新的ID(双向)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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