[R] Return Vector Component

arun smartpink111 at yahoo.com
Thu Nov 1 14:52:17 CET 2012


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. 


----- Original Message -----
From: frespider <frespider at hotmail.com>
To: r-help at r-project.org
Cc: 
Sent: Thursday, November 1, 2012 8:36 AM
Subject: [R] Return Vector Component

Hi, 

I am new  R user and I am still learn this fabulous software. I am stuck on
this part. I need to check 2 character vectors
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")

and 

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")

what I really want to do is loop over v2 and check if its interaction
component have any element in v1then return this component (i.e. v2 [4] =
"age_height_1_1" then check v1 which has "age_1" or "height_1"  then return 
"age_height_1_1") .

Is there a way to do that?

Thanks 



--
View this message in context: http://r.789695.n4.nabble.com/Return-Vector-Component-tp4648115.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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