[R] replace values in one df by values from key pairs in another df

2015-02-13 Thread arnaud gaboury
I have been searching for a while now, but can't put all pieces of the
puzzle together.

Goal : I want to replace all these kinds of patterns @U032FHV3S by
this @agreenmamba. In a more generic way, it is replacing 'id' by
user 'name'.

I have two df:
The first, 'history', is some message history where I want to do the
replacement. It is a chat history log,
The second one, idName, is a list of id:name. All are unique. This df
rows are exctracted from a bigger one called 'users' this way :
users[users$id %in% ext].


Please find below my two data.frames:
-
history - structure(list(name = c(xaeth, agreenmamba, poisonivy,
agreenmamba, cruzecontrol, poisonivy, poisonivy, vairis,
vairis, poisonivy), text = c(and people told that agent their
geocities experience would never amount to anything (the convo
yesterday) ,
@U032FHV3S http://youtu.be/oGIFublvDes, for me there is no such
question,
@U03AEKWTL|agreenmamba uploaded a file:
https://enlightened.slack.com/files/agreenmamba/F03KGRF3W/screenshot_2015-02-09-14-31-15.png|regarding:
should I stay or should I go?,
Lol., ha ha sorry, some testing with my irc client, anybody
knows how does \Passcode circuitry too hot. Wait for cool down to
enter another passcode.\ works?,
what is the cooldown time or how does it work...;, @U03AEKYL4:
what app are you talking about ?
)), .Names = c(name, text), class = c(data.table, data.frame
), row.names = c(NA, -10L)
--
idName - structure(list(id = c(U03AEKWTL, U032FHV3S, U03AEKYL4),
name = c(agreenmamba, poisonivy, vairis)), .Names = c(id,
name), sorted = name, class = c(data.table, data.frame
), row.names = c(NA, -3L))



1- I can find the pattern to be replaced in history this way :

ext - regmatches(history$text,regexpr('(?=@)[^|]{9}(?=|)',history$text,
perl = T)
 ext
[1] U032FHV3S U03AEKWTL U03AEKYL4

2- I can select the pairs id:name in my 'users' df:

 idName=users[users$id %in% ext]
  idname
1: U03AEKWTL agreenmamba
2: U032FHV3S   poisonivy
3: U03AEKYL4  vairis

3-  i can write a loop to make the changes:

 hist.txt - history$text
 for (i in 1:3){hist.txt - gsub(ext[i],idName[[2]][i], hist.txt, perl = T)}

But this will not work because of two things:
- 'idName' is not ordered the same way as 'ext'
- if a users speak twice it will leave with with some NA

May you have some hints how to process ?
I was thinking of maybe using sapply(myFun) to my 'history' df where
myFun would be the replacement process.
OR something like with assigning in my env each 'id' to its corresponding 'name'


Thank you for help

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] replace values in one df by values from key pairs in another df

2015-02-13 Thread arnaud gaboury
This is the wrong part of my code.


 idName=users[users$id %in% ext]
   idname
 1: U03AEKWTL agreenmamba
 2: U032FHV3S   poisonivy
 3: U03AEKYL4  vairis


Best is to use:

idNames - users[pmatch(ext, users$id, duplicates.ok = T)]. This leave
me with an ordered and duplicate entries :

 idNames - users[pmatch(ext, users$id, duplicates.ok = T)]
  idname
1: U03AEKYL4  vairis
2: U03AEKYL4  vairis
3: U03AEKWTL agreenmamba
4: U032FHV3S   poisonivy

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.