HI,
By splitting that code into smaller components:

res1<-strsplit(v2,"_") # will split the elements into substrings wherever  it 
matches substring "_".  Result will be a list
 head(res1)
#[[1]]
#[1] "sex" "1"  

#[[2]]
#[1] "sex" "2"  
#
[[3]]
#[1] "sex" "3"  
#
#[[4]]
#[1] "age"    "height" "1"      "1"     

#[[5]]
#[1] "age"    "height" "2"      "2"     

#[[6]]
#[1] "age"    "height" "3"      "3"     

#lists can be processed with lapply().  I used paste() to rearrange the split 
components so that it matches with v1
lapply(res1,function(x)
 paste(x[1],x[3],x[2],x[4],sep="_")) # in this case I change the order 
of components,  You can try changing the order or the number of 
#components and see how it works.
#For example
unlist(lapply(res1,function(x) paste(x[1],x[3],sep="_"))) #with just 1st and 
3rd component
# [1] "sex_NA" "sex_NA" "sex_NA" "age_1"  "age_2"  "age_3"  "age_4"  "age_5" 
 #[9] "age_6"  "age_1"  "age_2" 

 res2<-unlist(lapply(strsplit(v2,"_"),function(x) 
paste(x[1],x[3],x[2],x[4],sep="_")))
#Last part is indexing:
#grep() regular expression 
v1[1]
#[1] "age_1"
 grep(v1[1],res2)
#[1]  4 10           #this means 4th and 10 element of res2 matches with first 
element of v1[1]
  res2[4]
#[1] "age_1_height_1"
#The
 goal is to get the corresponding element of v2 that matches with v1.  
Since the order of res2 and v2 are the same, we can index it to get the 
matching v2 #elements.
v2[grep(v1[1],res2)]
#[1] "age_height_1_1" "age_height_1_7"

I hope this helps you.
A.K.
 



________________________________
From: Fares Said <frespi...@hotmail.com>
To: smartpink...@yahoo.com 
Sent: Thursday, November 1, 2012 2:09 PM
Subject: RE: [R] Return Vector Component



Hi,

I think this is better, but can you explain to me what  this code do 
function(x) paste(x[1],x[3],x[2],x[4],sep="_"),

Thanks 


> Date: Thu, 1 Nov 2012 10:17:56 -0700
> From: smartpink...@yahoo.com
> Subject: Re: [R] Return Vector Component
> To: frespi...@hotmail.com
> CC: r-help@r-project.org
> 
> HI,
> 
> May be this is a bit closer:
>  res1<-unlist(lapply(strsplit(v2,"_"),function(x) 
> paste(x[1],x[3],x[2],x[4],sep="_")))
>  v1[1]
> #[1] "age_1"
> 
> v2[grep(v1[1],res1)]
> #[1] "age_height_1_1" "age_height_1_7"
> 
>  v1[8]
> #[1] "height_2"
> v2[grep(v1[8],res1)]
> #[1] "age_height_2_2"
> A.K.
> 
> 
> 
> 
> ----- Original Message -----
> From: frespider <frespi...@hotmail.com>
> To: r-help@r-project.org
> Cc: 
> Sent: Thursday, November 1, 2012 10:20 AM
> Subject: Re: [R] Return Vector Component
> 
> 
> 
> Hi A.K.
> 
> Thank you so much for replying this could work but the problem you recreate 
> the newv1, to match v2 but I don't want to do that. I just want to check  if 
> v2 has that component from v1
> 
> THanks 
> Date: Thu, 1 Nov 2012 06:32:03 -0700
> From: ml-node+s789695n4648123...@n4.nabble.com
> To: frespi...@hotmail.com
> Subject: Re: Return Vector Component
> 
> 
> 
>     Hi,
> 
> 
> Not sure whether I understand it correctly.
> 
> v1 <- 
> c("age_1","age_2","age_3","age_4","age_5","age_6","height_1","height_2","height_3","height_4","height_5","height_6","height_7","height_8")
>  
> 
> v2 <- 
> c("sex_1","sex_2","sex_3","age_height_1_1","age_height_2_2","age_height_3_3","age_height_4_4","age_height_5_5","age_height_6_6",
> 
>          "age_height_1_7","age_height_2_8") 
> 
> v3<-gsub("(age{0,1}\\_).*","\\1",v1)
> 
> v4<-gsub("age{0,1}\\_(.*)","\\1",v1)
> 
> v5<-gsub("(height{0,1}\\_).*","\\1",v1)
> 
> v6<-gsub("height{0,1}\\_(.*)","\\1",v1)
> 
> newv1<-paste0(v3[grep("age",v3)],v5[grep("height",v5)],v4[!v4%in%v4[grep("height",v4)]],"_",v6[!v6%in%v6[grep("age",v6)]])
> 
> newv1[newv1%in%v2]
> 
> #[1] "age_height_1_1" "age_height_2_2" "age_height_3_3" "age_height_4_4"
> 
> #[5] "age_height_5_5" "age_height_6_6" "age_height_1_7" "age_height_2_8"
> 
> A.K.
> 
> 
> 
>     
>     
>     
>     
> 
>     
> 
>     
>     
>         If you reply to this email, your message will be added to the 
> discussion below:
>         
> http://r.789695.n4.nabble.com/Return-Vector-Component-tp4648115p4648123.html
>     
>     
>         
>         To unsubscribe from Return Vector Component, click here.
> 
>         NAML
>                                
> 
> 
> 
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/Return-Vector-Component-tp4648115p4648126.html
> Sent from the R help mailing list archive at Nabble.com.
>     [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help@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.
>

______________________________________________
R-help@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.

Reply via email to