-1

I have some data that are in a code as battle ship game, like this:A0,A1,B0,B4,K12 and I want to transform these into coordinate points. The letter should be the x-coordinate and the number the y-coordinate. Besides that, I should transform the letters in numbers to multiply them. Like that:

A0 = 0 , 0;   
A1 = 0 , 15;   
A2 = 0 , 30; 
B3 = 15 , 45
NSano
  • 13
  • 1

5 Answers5

2

Here you go:

BattleshipConversion <- function(mystring)
{
  return(c(which(LETTERS==substr(mystring,1,1))-1,as.integer(substr(mystring,2,3)))*15)
}

Result:

>BattleshipConversion("B1") 
15 15
>BattleshipConversion("A10")
0 150

So what is happening above?

  • LETTERS is an R pre-generated vector of capital letters. which takes the index position of the letter in that vector, so which(LETTERS=='A') will give 1. We subtract 1 from that.
  • substr is a function that extracts a substring from a string, taking string, start and stop as arguments. counting starts with the first element, which in R is 1. substring(mystring,1,1) takes the first character element of mystring and stops there.
  • as.integer simply converts the 1-2 digit integer stored as character into a proper integer format.
  • we save it all in a combined vector using c(), and everything gets multiplied by 15, per the OP's specification
  • the function returns the result.

Note that this assumes your input string is correctly formatted. It will only work up to Z and 99, i.e. will fail on an AA14 or B101. You may want to add in some safeguards.

Serban Tanasa
  • 3,592
  • 2
  • 23
  • 45
1

Say you have these positions:

pos<-c("A0","A1","A2","B3","K12")

You can:

require(data.table) #just to use tstrsplit
res<-setNames(as.data.frame(tstrsplit(pos,"(?<=[A-Z])",perl=TRUE),stringsAsFactors=FALSE),c("x","y"))
res[[1]]<-(match(res[[1]],LETTERS)-1)*15
res[[2]]<-as.numeric(res[[2]])*15
cbind(pos,res)
#  pos   x   y
#1  A0   0   0
#2  A1   0  15
#3  A2   0  30
#4  B3  15  45
#5 K12 150 180   
nicola
  • 24,005
  • 3
  • 35
  • 56
1

This is vectorized and can be extended to double letters easily:

fun <- function(s) {
  x <- gsub("[[:digit:]]", "", s) #remove numbers
  y <- gsub("[[:alpha:]]", "", s) #remove letters

  x <- match(x, LETTERS) - 1 #match against letters
  y <- as.integer(y)
  cbind(x = x * 15, y = y * 15)
}

fun(c("A0", "A1", "A2", "B3"))
#      x  y
#[1,]  0  0
#[2,]  0 15
#[3,]  0 30
#[4,] 15 45
Roland
  • 127,288
  • 10
  • 191
  • 288
0

Here is a dplyr answer

library(dplyr)
library(tidyr)
library(rex)

template = rex(capture(letters),
               capture(numbers) )

coordinates = c("A0","A1","B0","B4","K12")

letter_frame = 
  data_frame(LETTERS,
             x_small = 1:26)

result = 
  data_frame(coordinate = coordinates) %>%
  extract(coordinate, c("letter", "y_small"), template, convert = TRUE) %>%
  left_join(letter_frame) %>%
  mutate(x = x_small*15,
         y = y_small*15)
bramtayl
  • 4,004
  • 2
  • 11
  • 18
-1
BSconverter <- function(str){ 
  let <- substr(str,1,1)
  num <- as.integer(substr(str,2,nchar(str))) * 15
  letnum <- (which(LETTERS==let)-1) * 15 
  c(letnum, num)

}


> BSconverter("K12")
[1] 150 180
maRtin
  • 6,336
  • 11
  • 43
  • 66
  • Thank you very much for your idea :), but my coordinate data are a string composed by letter and number ("A0", as an example), so I would have to separate them to use your code. Do you know how to do it? – NSano Apr 28 '16 at 14:30