Re: [R] Converting english words to numeric equivalents
On 28 Jul 2008, at 12:23, Hans-Joerg Bibiko wrote: How about this? unletter <- function(word) { gsub('-64',' ',paste(sprintf("%02d",utf8ToInt(tolower(word)) - 96),collapse='')) } unletter("abc") [1] "010203" unletter("Aw") [1] "0123" unletter("I walk to school") [1] "09 23011211 2015 190308151512" I do not know precisely what do you want to do. With: as.double(unlist(strsplit(unletter("I walk to school")," "))) you will get a numeric vector out of the string. But this leads to a problem with large words like: as.double(unlist(strsplit(unletter("schoolschool")," "))) [1] 1.903082e+23 Thus I would suggest if there's a need to mirror words as numeric values and the numeric values haven't a meaning to parse your text in beforehand to build a hash (a list) of all distinct words in your text and assign a number to each word. This would end up in a list à la: words <- ("abc" = 1, "I" = 2, "go" = 3, etc.) After that you can access these numeric values via: words['go'] $go [1] 3 --Hans __ R-help@r-project.org mailing list 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] Converting english words to numeric equivalents
How about this? unletter <- function(word) { gsub('-64',' ',paste(sprintf("%02d",utf8ToInt(tolower(word)) - 96),collapse='')) } unletter("abc") [1] "010203" unletter("Aw") [1] "0123" unletter("I walk to school") [1] "09 23011211 2015 190308151512" --Hans __ R-help@r-project.org mailing list 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] Converting english words to numeric equivalents
As you point out, my proposed function does not verify an equivalence, and it is not restricted to english words. I'm probably too keen to see questions that call for an answer with relaxed assumptions (understand, that i can manage). Cheers, baptiste On 28 Jul 2008, at 10:47, (Ted Harding) wrote: On 28-Jul-08 09:29:22, baptiste auguie wrote: Here is my attempt at this (taking a specific understanding of the ill- defined "equivalence" relation), unletter <- function(word){ word.broken <- strsplit(word, NULL) set.of.numbers <- sapply(word.broken[[1]], function(let) which(let == letters)) paste(set.of.numbers, sep="", collapse="") } unletter("abc") # "123" Ay[yes, I understand] unletter("xyz") # "242526" bdbebf unletter("salut") # "191122120" aikbut[or do I?] Hello, I am trying to convert english words to numeric equivalents, e.g., abc to 123. Is there a function or library or package for doing this in R? If not, can it be done easily in R? _ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag __ R-help@r-project.org mailing list 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. E-Mail: (Ted Harding) <[EMAIL PROTECTED]> Fax-to-email: +44 (0)870 094 0861 Date: 28-Jul-08 Time: 10:47:13 -- XFMail -- __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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] Converting english words to numeric equivalents
On 28-Jul-08 09:29:22, baptiste auguie wrote: > Here is my attempt at this (taking a specific understanding of the ill- > defined "equivalence" relation), > > unletter <- function(word){ > > word.broken <- strsplit(word, NULL) > set.of.numbers <- sapply(word.broken[[1]], function(let) which(let == > letters)) > paste(set.of.numbers, sep="", collapse="") > } > > unletter("abc") > # "123" Ay[yes, I understand] > unletter("xyz") > # "242526" bdbebf > unletter("salut") > # "191122120" aikbut[or do I?] Hello, I am trying to convert english words to numeric equivalents, e.g., abc to 123. Is there a function or library or package for doing this in R? If not, can it be done easily in R? >>> > > _ > > Baptiste Auguié > > School of Physics > University of Exeter > Stocker Road, > Exeter, Devon, > EX4 4QL, UK > > Phone: +44 1392 264187 > > http://newton.ex.ac.uk/research/emag > > __ > R-help@r-project.org mailing list > 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. E-Mail: (Ted Harding) <[EMAIL PROTECTED]> Fax-to-email: +44 (0)870 094 0861 Date: 28-Jul-08 Time: 10:47:13 -- XFMail -- __ R-help@r-project.org mailing list 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] Converting english words to numeric equivalents
Here is my attempt at this (taking a specific understanding of the ill- defined "equivalence" relation), unletter <- function(word){ word.broken <- strsplit(word, NULL) set.of.numbers <- sapply(word.broken[[1]], function(let) which(let == letters)) paste(set.of.numbers, sep="", collapse="") } unletter("abc") # "123" unletter("xyz") # "242526" unletter("salut") # "191122120" Hello, I am trying to convert english words to numeric equivalents, e.g., abc to 123. Is there a function or library or package for doing this in R? If not, can it be done easily in R? _ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag __ R-help@r-project.org mailing list 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] Converting english words to numeric equivalents
2008/7/28 Rolf Turner <[EMAIL PROTECTED]>: > > On 28/07/2008, at 12:32 PM, oscar Linares wrote: > >> Hello, >> >> I am trying to convert english words to numeric equivalents, e.g., abc to >> 123. Is there a function or library or package for doing this in R? If not, >> can it be done easily in R? > > Your question is very ill-posed. What ``english word'' would correspond to > 0? > What would the ``english word'' xyz (?!?!) correspond to? > > If you can phrase a coherent question, it can probably be answered. if abc maps to 123, then xyz maps to 242,526. Or possibly 2,676. this reminds me of the 'analogy problems' in Metamagical Themas by Douglas Hofstadter. Example, if abc goes to abd, what does pqrs go to? pqrt or pqrd? pqtu? qrst? Or something else? Barry __ R-help@r-project.org mailing list 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] Converting english words to numeric equivalents
On 28/07/2008, at 12:32 PM, oscar Linares wrote: Hello, I am trying to convert english words to numeric equivalents, e.g., abc to 123. Is there a function or library or package for doing this in R? If not, can it be done easily in R? Your question is very ill-posed. What ``english word'' would correspond to 0? What would the ``english word'' xyz (?!?!) correspond to? If you can phrase a coherent question, it can probably be answered. cheers, Rolf Turner ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ R-help@r-project.org mailing list 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.
[R] Converting english words to numeric equivalents
Hello, I am trying to convert english words to numeric equivalents, e.g., abc to 123. Is there a function or library or package for doing this in R? If not, can it be done easily in R? Thanks, Oscar [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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.