Skip to content

transform 3 numeric vectors empty of 0/1

3 messages · Arnaud Michel, David L Carlson, arun

#
Dear all,
Without a loop, I would like transform 3 numeric vectors empty of 0/1 of 
same length
Vec1 : transform 1 to A and 0 to ""
Vec2 : transform 1 to B and 0 to ""
Vec3 : transform 1 to C and 0 to ""
to obtain only 1 vector Vec who is the paste of the 3 vectors (Ex : ABC, 
BC, AC, AB,...)
Any idea ?
Thank you for your help
#
Try this
ifelse(Vec3,"C",""))
 [1] "ABC" "ABC" ""    "AC"  "AB"  "ABC" "A"   "B"   "ABC" "AC"  "B"
"A"  
[13] "AB"  "C"   "B"  

-------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Arnaud Michel
Sent: Tuesday, June 18, 2013 11:58 AM
To: r-help at r-project.org
Subject: [R] transform 3 numeric vectors empty of 0/1

Dear all,
Without a loop, I would like transform 3 numeric vectors empty of
0/1 of 
same length
Vec1 : transform 1 to A and 0 to ""
Vec2 : transform 1 to B and 0 to ""
Vec3 : transform 1 to C and 0 to ""
to obtain only 1 vector Vec who is the paste of the 3 vectors (Ex :
ABC, 
BC, AC, AB,...)
Any idea ?
Thank you for your help
#
Hi,
You could also use:
dat1<- data.frame(Vec1,Vec2,Vec3)
Reduce(paste0,lapply(seq_len(ncol(dat1)),function(i) {x1<- as.character(factor(dat1[,i],labels=c("",LETTERS[i])))}))
# [1] "ABC" "ABC" ""??? "AC"? "AB"? "ABC" "A"?? "B"?? "ABC" "AC"? "B"?? "A"? 
#[13] "AB"? "C"?? "B"? 
#or
library(plyr)
library(car)
dat2<-colwise(recode)(dat1,recodes="1='ABC';0=''")
?Reduce(paste0,lapply(seq_len(ncol(dat2)),function(i) substr(dat2[,i],i,i)))
# [1] "ABC" "ABC" ""??? "AC"? "AB"? "ABC" "A"?? "B"?? "ABC" "AC"? "B"?? "A"? 
#[13] "AB"? "C"?? "B"? 

#or
?paste0(recode(Vec1,'1="A";0=""'),recode(Vec2,'1="B";0=""'),recode(Vec3,'1="C";0=""'))
# [1] "ABC" "ABC" ""??? "AC"? "AB"? "ABC" "A"?? "B"?? "ABC" "AC"? "B"?? "A"? 
#[13] "AB"? "C"?? "B"? 



#Speed comparisons:
set.seed(42)
?Vec1 <- sample(0:1, 1e6, replace=TRUE)
?Vec2 <- sample(0:1, 1e6, replace=TRUE)
?Vec3 <- sample(0:1, 1e6, replace=TRUE)
system.time(res<- paste0(ifelse(Vec1,"A",""), ifelse(Vec2,"B",""),ifelse(Vec3,"C","")))
# user? system elapsed 
#? 2.028?? 0.020?? 2.051 

set.seed(42)
system.time(res1<-Reduce(paste0,lapply(1:3,function(i) {x1<- as.character(factor(sample(0:1,1e6,replace=TRUE),labels=c("",LETTERS[i])))})))
?#user? system elapsed 
?# 1.216?? 0.000?? 1.219 


dat1<- data.frame(Vec1,Vec2,Vec3)
system.time(res2<-Reduce(paste0,lapply(seq_len(ncol(dat1)),function(i) {x1<- as.character(factor(dat1[,i],labels=c("",LETTERS[i])))})))
?#? user? system elapsed 
?# 1.256?? 0.000?? 1.257 

system.time({
dat2<-colwise(recode)(dat1,recodes="1='ABC';0=''")
?res3<-Reduce(paste0,lapply(seq_len(ncol(dat2)),function(i) substr(dat2[,i],i,i)))
})
# user? system elapsed 
#? 2.232?? 0.008?? 2.249 
?system.time(res4<-paste0(recode(Vec1,'1="A";0=""'),recode(Vec2,'1="B";0=""'),recode(Vec3,'1="C";0=""')))
?#? user? system elapsed 
?# 1.716?? 0.012?? 1.728 


identical(res1,res)
#[1] TRUE
identical(res,res2)
#[1] TRUE
?identical(res,res3)
#[1] TRUE
identical(res,res4)
#[1] TRUE

A.K.


----- Original Message -----
From: David Carlson <dcarlson at tamu.edu>
To: 'Arnaud Michel' <michel.arnaud at cirad.fr>; r-help at r-project.org
Cc: 
Sent: Tuesday, June 18, 2013 1:08 PM
Subject: Re: [R] transform 3 numeric vectors empty of 0/1

Try this
ifelse(Vec3,"C",""))
[1] "ABC" "ABC" ""? ? "AC"? "AB"? "ABC" "A"?  "B"?  "ABC" "AC"? "B"
"A"? 
[13] "AB"? "C"?  "B"? 

-------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Arnaud Michel
Sent: Tuesday, June 18, 2013 11:58 AM
To: r-help at r-project.org
Subject: [R] transform 3 numeric vectors empty of 0/1

Dear all,
Without a loop, I would like transform 3 numeric vectors empty of
0/1 of 
same length
Vec1 : transform 1 to A and 0 to ""
Vec2 : transform 1 to B and 0 to ""
Vec3 : transform 1 to C and 0 to ""
to obtain only 1 vector Vec who is the paste of the 3 vectors (Ex :
ABC, 
BC, AC, AB,...)
Any idea ?
Thank you for your help