Home > database >  Converting letter vector into numeric vector
Converting letter vector into numeric vector

Time:10-06

If I want to convert the letter vector c("A","B","C") into c(10,20,30), what function could I use?

Sorry for asking a question that seems to be trivial. I am a self-taught beginner and I am still getting familiar with the functions.

Edit:

I explain why I ask such strange question.

So here is the background:

A standard deck of playing cards can be created in R as a data frame with the following command.

Note that D = Diamond, C = Club, H = Heart, S = Spade

deck <- data.frame(
suit = rep(c("D","C","H","S"), 13),

rank = rep(2:14, 4)
11 = Jack, 12 = Queen, 13 = King, 14 = Ace
) 

A poker hand is a set of five playing cards. Sample a poker hand using the data frame deck and name it as hand.

hand<-deck[sample(nrow(deck),5),]
hand

A flush is a hand that contains five cards all of the same suit. Create a logical value named is.flush which is TRUE if and only if hand is a flush.

is.flush<-length(unique(hand[,1]))==1
is.flush

And here starts the problem:

"A straight is a hand that contains five cards of sequential rank. Note that both

A K Q J 10 and 5 4 3 2 A are considered to be straight, but Q K A 2 3 is not. Create a logical value named is. straight which is TRUE if and only if the hand is
straight." Hint: The all() function could be useful.

So here is my attepmt:

I can set:

y <- read.table(text = "
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10")

apply(y, 1, function(x) all(diff(sort(x[ x != 2 ])) == 1))

Then I can have a TRUE FALSE value.

But I cannot input letters in the function above.

Hence I am stuck here, and I have to convert the letter to numbers.

(Unless there is a smarter way)

P.S.

The background code I have so far:

deck <- data.frame(
suit = rep(c("D","C","H","S"), 13),
rank = rep(2:14, 4)
) 
deck

hand<-deck[sample(nrow(deck),5),]
hand

is.flush<-length(unique(hand[,1]))==1
is.flush

CodePudding user response:

Sounds like you want case_when inside a custom function

library(tidyverse)

my_func <- function(letter) { 
      case_when(letter == 'A' ~ 10,
                letter == 'B' ~ 20,
                letter == 'C' ~ 30, 
                TRUE ~ 0)
      }
my_func(c("A","B","C"))

Will give you

[1] 10 20 30

CodePudding user response:

If you want to map each letter to an arbitrary output value, you can use a named vector as a dictionary, for example:

dictionary <- (1:26) * 10          # 10, 20, 30 .. 260
names(dictionary) <- LETTERS       # built-in vector of uppercase letters
dictionary
  A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V 
 10  20  30  40  50  60  70  80  90 100 110 120 130 140 150 160 170 180 190 200 210 220 
  W   X   Y   Z 
230 240 250 260

You can then use letters to index the dictionary and return the mapped value:

test <- c("B", "L", "A")
dictionary[test]
  B   L   A 
 20 120  10 

The function that is actually performing the mapping here is the [ operator, see the Extract docs.

CodePudding user response:

You could do:

library(tidyverse)

x <- c("A","B","C") 

recode(x, A = 10, B = 20, C = 30, .default = 0)
  •  Tags:  
  • r
  • Related