Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extracting Numeric Values from Words
My.Words <- read.csv("http://www.oberlin.edu/faculty/cdesante/assets/downloads/Randwords.csv")
My.Words$RANDOM.WORDS <- as.character(My.Words$RANDOM.WORDS)
#Step 1: Split Character Vectors into Sub-strings:
strsplit(My.Words$RANDOM.WORDS, "")
Split.List <- strsplit(My.Words$RANDOM.WORDS, "")
#Step 2: Assign a numeric value to each of the 26 letters:
LETTERS
rank(LETTERS)
Letter.Values<- rank(LETTERS)
names(Letter.Values) <- LETTERS
Letter.Values
#Step 3: Extract the value of each word by summing the letters' values
Word.Sums <- unlist(lapply(Split.List, function(Word){sum(Letter.Values[Word])}))
Word.Sums
#Step 4: Which words have the highest and lowest values?
My.Words$RANDOM.WORDS[Word.Sums==max(Word.Sums)]
My.Words$RANDOM.WORDS[Word.Sums==min(Word.Sums)]
@vkryukov

This comment has been minimized.

Show comment
Hide comment
@vkryukov

vkryukov Dec 19, 2012

Steps 2 and 3 look very complicated. You can use the following one-liner

Word.Sums <- sapply(Split.List, function(word) sum(match(word, LETTERS)))

vkryukov commented Dec 19, 2012

Steps 2 and 3 look very complicated. You can use the following one-liner

Word.Sums <- sapply(Split.List, function(word) sum(match(word, LETTERS)))
@vkryukov

This comment has been minimized.

Show comment
Hide comment
@vkryukov

vkryukov Dec 19, 2012

For step 4, you could use which.min and which.max

vkryukov commented Dec 19, 2012

For step 4, you could use which.min and which.max

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment