[R] Convert chr pieces to numbers that have specific values defined by 2 vectors

arun smartpink111 at yahoo.com
Mon Sep 2 23:34:52 CEST 2013


Hi,
On a bigger dataset:

#Speed:
set.seed(285)
dat1<- as.data.frame(matrix(paste0(sample(1:10,69*3e5,replace=TRUE),sample(LETTERS[1:10],69*3e5,replace=TRUE)),ncol=69,nrow=3e5),stringsAsFactors=FALSE)
length(unique(unlist(dat1)))
#[1] 100

set.seed(3490)
dat2<- data.frame(old=unique(unlist(dat1)),new=sample(1:100,100,replace=FALSE),stringsAsFactors=FALSE)
 system.time({dat1New<-as.data.frame(array(dat2[,2][match(as.matrix(dat1),dat2[,1])],dim= dim(dat1),dimnames=dimnames(dat1)))})
 #user  system elapsed 
 # 1.480   0.236   1.719 

A.K.


----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: tobias schlager <tobebryant at me.com>
Cc: R help <r-help at r-project.org>
Sent: Monday, September 2, 2013 5:28 PM
Subject: Re: [R] Convert chr pieces to numbers that have specific values defined	by 2 vectors

Hi,

You may try this:

set.seed(285)
dat1<- as.data.frame(matrix(paste0(sample(1:10,100,replace=TRUE),sample(LETTERS[1:10],100,replace=TRUE)),10,10),stringsAsFactors=FALSE)

set.seed(3490)
dat2<- data.frame(old=unique(unlist(dat1)),new=sample(1:100,63,replace=FALSE),stringsAsFactors=FALSE)
 dat1New<-as.data.frame(array(dat2[,2][match(as.matrix(dat1),dat2[,1])],dim= dim(dat1),dimnames=dimnames(dat1)))
dat1New
#   V1 V2 V3  V4 V5 V6 V7 V8 V9 V10
#1  68 68 68  14 48 28 30 27 17  39
#2  71  7 64  93 25 67 61 93 67  31
#3  58 27 17  37 71 31 16 51 69  19
#4  30 71 75  43 86 27 47 35 71  22
#5  22 38 59  55  6 11 10 32 54  92
#6  63 20 88  65 17 12 48 73 54  74
#7  19 61 94  99 54 83 10  7 44  49
#8   5  2 58 100 43 63 12 10 97   2
#9  63 94 91  79 95 54 57 32 94  84
#10  5 60 65  69 50 46 70 12 98  54


A.K.




----- Original Message -----
From: tobias schlager <tobebryant at me.com>
To: r-help at r-project.org
Cc: 
Sent: Monday, September 2, 2013 3:29 PM
Subject: [R] Convert chr pieces to numbers that have specific values defined    by 2 vectors

Dear all, 

I think this is an easy task, but I don't know how to do it. Specifically, I have 69 columns with 300.000 rows. In each cell there is a code like 
"2E3", "4RR", etc.

I now have a list that replaces this with values, e.g., 
old    new
2E3         5
4RR        3
etc. 

The list ist about 1600 rows long, so also to extensive for normal solutions

Do you how to do that? It would be great if you could help me, 
best, 
tobebryant
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list