[R] Return Vector Component

arun smartpink111 at yahoo.com
Thu Nov 1 18:17:56 CET 2012


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 <frespider at hotmail.com>
To: r-help at 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+s789695n4648123h64 at n4.nabble.com
To: frespider at 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 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