Reputation: 85
I'm a noob with R. I want the alphabet index of each letter in a word. I don't understand what I'm doing wrong, since the individual command works perfectly...
word <- "helloworld"
l <- numeric(nchar(word))
for (i in 0:nchar(word)) {
l[i] <- match(substr(word,i,i+1), letters)
}
l
returns a weird [1] NA NA NA NA NA NA NA NA NA 4
when match(substr(word,0,1), letters)
returns the appropriate [1] 8
Upvotes: 1
Views: 3150
Reputation: 3223
You tested the only constellation that could work...
But you don't have to use a loop here. as Richard Telford suggested, you can transform your string into a character-vector. Then you match every element of this vector to the letters vector
lapply(strsplit(word, ""), match, letters)
Upvotes: 0
Reputation: 24520
The error lies in the i+1
: you are getting two character strings and so no match is found. Use substring
which is vectorized:
match(substring(word,1:nchar(word),1:nchar(word)),letters)
#[1] 8 5 12 12 15 23 15 18 12 4
Another (nerdy) way is to get the offset of the ASCII value of each character to the value of the char a
:
as.integer(charToRaw(word))-as.integer(charToRaw("a"))+1
#[1] 8 5 12 12 15 23 15 18 12 4
Upvotes: 1
Reputation: 9933
The problem with your code is two fold.
First R indices start at 1 so when i = 0, l[i] is undefined.
Second, it doesn't just pull off a single letter at a time
i = 1
substr(word,i,i+1)
[1] "he"
A different approach
setNames(1:26, letters)[ strsplit("hello", NULL )[[1]] ]
Upvotes: 3