Skip to content

quick help needed: split a number and "find and replace" type of function that works like in MS excel

3 messages · Ram H. Sharma, Steve Lianoglou

#
Hi,

There are a couple of ways to do what you want.

I'll provide the fodder and let you finish the implementation.
On Sun, May 1, 2011 at 4:26 PM, Ram H. Sharma <sharma.ram.h at gmail.com> wrote:
You can convert your numbers to characters, if you like. Using your
dataset, consider:

R> ct1.char <- as.character(mydf$CT1)
R> ct1.char <- strsplit(as.character(mydf$CT1), '')
R> ct1a <- sapply(ct1.char, '[', 1)  ## "non-obvious" use of '[' as
R> ct1b <- sapply(ct1.char, '[', 2)  ## a function is intentional :-)
R> head(data.frame(ct1a=ct1a, ct1b=ct1b))
  ct1a ct1b
1    3    4
2    1    4
3    2    3
4    1    4
5    3    4
6    2    3
Try gsub:

R> head(ct1a)
[1] "3" "1" "2" "1" "3" "2"

R> head(gsub("1", "A", ct1a))
[1] "3" "A" "2" "A" "3" "2"

or you can use a "translation table"

R> xlate <- c('1'='A', '2'='B', '3'='C')
R> head(xlate[ct1a])
  3   1   2   1   3   2
"C" "A" "B" "A" "C" "B"

You might also consider not converting your original data into
characters and splitting off the integers -- you can use modulo
arithmetic to get each digit, ie:

R> head(mydf$CT1)
[1] 34 14 23 14 34 23

## First digit
R> head(as.integer(mydf$CT1 / 10))
[1] 3 1 2 1 3 2

## Second digit
R> head(mydf$CT1 %% 10)
[1] 4 4 3 4 4 3

There's some food for thought ..

-steve