R:将电子邮件地址转换为唯一的整数

时间:2012-03-18 15:02:18

标签: r char int converter

R初学者似乎是一个非常简单的问题: 我有很多电子邮件日志,我已经以R格式读入R:

>log1
    Date        Time            From                 To
1   2000-01-01  00:00:00    bob@mail.com            test1@mail.com
2   2000-01-02  01:00:00    carolyn @mail.com       test2@mail.com
3   2000-01-03  02:00:00    chris@mail.com          test3@mail.com
4   2000-01-04  03:00:00    chris @mail.com         test4@mail.com
5   2000-01-05  04:00:00    alan@mail.com           test5@mail.com
6   2000-01-06  05:00:00    alan.@mail.com          test6@mail.com

我需要将log1 $ From和log1 $ To更改为全局唯一数字标识符,这样当我在其他日志中读取时,任何给定的电子邮件地址都将获得与先前日志相同的标识符。

我试过了:

id <- as.numeric(as.character(log1[,3])))
id<-as.numeric(levels(log1[,3])))
id <- charToRaw(log1[,4]), base=16)

有些灵魂请帮助我 - 谢谢!

道歉应该包含这个:

 Date=c( "01/01/2000" ,"02/01/2000" ,"03/01/2000", "04/01/2000" ,"05/01/2000" ,"06/01/2000","07/01/2000","08/01/2000",
    "09/01/2000","10/01/2000","11/01/2000", "12/01/2000" ,"13/01/2000", "14/01/2000", "15/01/2000","16/01/2000"
    ,"17/01/2000","18/01/2000","19/01/2000","20/01/2000","01/01/2000","02/01/2000")
    Time=c("00:00:00","01:00:00","02:00:00", "03:00:00" ,"04:00:00" ,"05:00:00", "06:00:00" ,"07:00:00", "08:00:00", "09:00:00" ,"10:00:00",
    "11:00:00", "12:00:00","13:00:00", "14:00:00","15:00:00","16:00:00","17:00:00","18:00:00","19:00:00","00:00:00" ,"00:00:00")
    From=c("bob.shults@mail.com","carolyn.green@mail.com","chris.long@mail.com","christi.nicolay@mail.com","alan.aronowitz@mail.com","alan.comnes@mail.com",
    "dab@sprintmail.com","ana.correa@mail.com","andrew.fastow@mail.com","elena.kapralova@mail.com","bob.shults@mail.com","carolyn.green@mail.com",
    "chris.long@mail.com","christi.nicolay@mail.com","alan.aronowitz@mail.com","alan.comnes@mail.com","dab@sprintmail.com","ana.correa@mail.com",
    "andrew.fastow@mail.com","elena.kapralova@mail.com","bob.shults@mail.com","bob.shults@mail.com")
    To=c("ana.correa@mail.com","test2@mail.com","test3@mail.com","test4@mail.com","test5@mail.com","test6@mail.com","test7@mail.com",
    "test8@mail.com","test9@mail.com","test10@mail.com","test11@mail.com","test12@mail.com","test13@mail.com","test14@mail.com",
    "test15@mail.com","test16@mail.com","test17@mail.com","test18@mail.com","test19@mail.com","test20@mail.com","ana.correa@mail.com","ana.correa@mail.com")
    log<-data.frame(Date=Date,Time=Time,From=From,To=To)

尝试使用MD5生成全局唯一标识符:请注意ana.correa@mail.com的标识符在ID_to中是否正确匹配,但不在ID_from

ID_to<-data.frame()
    ID_from<-data.frame()

    for (i in 1:nrow(log)){
    to<-as.numeric(paste('0x', substr(rep(hmac('secret',log[i,4], algo='md5'), 2), c(1, 9, 17, 25), c(8, 16, 24, 32)),sep=""))
    (ID_to<-rbind(ID_to,to))

    from<-as.numeric(paste('0x', substr(rep(hmac('secret',log[i,3], algo='md5'), 2), c(1, 9, 17, 25),c(8, 16, 24, 32)),sep=""))
    (ID_from<-rbind(ID_from,from))

    }

    ID_to[,3]<-paste(ID_to[,1],ID_to[,2], sep="")
    ID_from[,3]<-paste(ID_from[,1],ID_from[,2], sep="")

    edgelist<-data.frame(ID_from[,3],log[,3],ID_to[,3],log[,4],log[,1],log[,2])
    print(edgelist)
    ID_from...3.                 log...3.           ID_to...3.            log...4.   log...1. log...2.
    27488842661591306920      bob.shults@mail.com 18727221862165338513 ana.correa@mail.com 01/01/2000 00:00:00
    38124472891255273775   carolyn.green@mail.com  1251903296725454474      test2@mail.com 02/01/2000 01:00:00
    29070047663451376630      chris.long@mail.com 17074276751156451031      test3@mail.com 03/01/2000 02:00:00
    8261398433828474582 christi.nicolay@mail.com  1563683670909194033      test4@mail.com 04/01/2000 03:00:00
    18727221862165338513  alan.aronowitz@mail.com 26735368323826533112      test5@mail.com 05/01/2000 04:00:00
    5680838251168988404     alan.comnes@mail.com  2923605896229594830      test6@mail.com 06/01/2000 05:00:00
    2351312285811012730       dab@sprintmail.com 17171333544033270402      test7@mail.com 07/01/2000 06:00:00
    328278708432069254      ana.correa@mail.com 33840664403556851587      test8@mail.com 08/01/2000 07:00:00
    1127901879852039037   andrew.fastow@mail.com  1973548136161209824      test9@mail.com 09/01/2000 08:00:00
    7349515121496417787 elena.kapralova@mail.com  5680838251168988404     test10@mail.com 10/01/2000 09:00:00
    27488842661591306920      bob.shults@mail.com   328278708432069254     test11@mail.com 11/01/2000 10:00:00
    38124472891255273775   carolyn.green@mail.com  1127901879852039037     test12@mail.com 12/01/2000 11:00:00
    29070047663451376630      chris.long@mail.com 27488842661591306920     test13@mail.com 13/01/2000 12:00:00
    8261398433828474582 christi.nicolay@mail.com 38124472891255273775     test14@mail.com 14/01/2000 13:00:00
    18727221862165338513  alan.aronowitz@mail.com 29070047663451376630     test15@mail.com 15/01/2000 14:00:00
    5680838251168988404     alan.comnes@mail.com  8261398433828474582     test16@mail.com 16/01/2000 15:00:00
    2351312285811012730       dab@sprintmail.com  2351312285811012730     test17@mail.com 17/01/2000 16:00:00
    328278708432069254      ana.correa@mail.com  7349515121496417787     test18@mail.com 18/01/2000 17:00:00
    1127901879852039037   andrew.fastow@mail.com 41762759923562968495     test19@mail.com 19/01/2000 18:00:00
    7349515121496417787 elena.kapralova@mail.com 24894056753582090007     test20@mail.com 20/01/2000 19:00:00
    27488842661591306920      bob.shults@mail.com 18727221862165338513 ana.correa@mail.com 01/01/2000 00:00:00
    27488842661591306920      bob.shults@mail.com 18727221862165338513 ana.correa@mail.com 02/01/2000 00:00:00

尝试使用等级/因子方法:

收到错误:

log <- union(levels(log[,3]), levels(log[,4]))
>Error in emails[, 3] : incorrect number of dimensions

3 个答案:

答案 0 :(得分:2)

您可以使用MD5生成全局唯一标识符,因为它具有非常低的冲突概率,但由于它的输出是128位,因此您需要一些数字来表示它(32位R中的四个整数,两个整数) 64位R)。但是,使用短数字向量应该很容易处理。

以下是如何为电子邮件地址(或任何其他字符串)生成此类四个整数的向量:

library(digest)
email <- 'test1@gmail'
as.numeric(paste('0x', substr(rep(hmac('secret56f8a7', email, algo='md5'), 4), c(1, 9, 17, 25), c(8, 16, 24, 32)), sep=''))

您可以使用algo='crc32'并获得一个整数,但不建议这样做,因为CRC更容易发生冲突。

答案 1 :(得分:1)

您需要为日志中的每封电子邮件创建唯一ID。一种方法是计算每封电子邮件的crc校验和,并将其用作标识符,但它的编号非常长。或者你可以在R中实现一个hashmap,并使电子邮件成为hashmap的关键。

答案 2 :(得分:1)

我认为这会做你想要的,而且效率很高,你可以只使用基础套件......

步骤:

1.将两列都转换为因子

2.以完全相同的方式联系因子级别,以便每封电子邮件在因子级别中具有唯一ID。

3.将每列中的条目更改为与其因子级别对应的数字。因此,我们可以通过简单地在两列中查找“1”来确定“test1@gmail.com”发送和接收电子邮件的时间。

log1$From <- as.factor(log1$From) 
log1$To <- as.factor(log1$To) 
emails <- union(levels(log1$From), levels(log1$To))
levels(log1$From) <- emails
levels(log1$To) <- emails
log1$From <- as.numeric(log1$From) 
log1$To <- as.numeric(log1$To)

保留原始电子邮件地址的记录可能是个好主意,就像我在这里所做的那样。然后,如果您对发送的电子邮件test1@gmail.com感兴趣:

log1[log1$From == which(emails == "test1@gmail.com"), ]

应该做的伎俩!你可以写一个程序,让它看起来更干净......