[R] part of name to Date

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Tue Mar 1 16:07:07 CET 2005


Here is one way

x <- c( "VGT1_CONTR_B020020301.H0V0.MIR", 
	"VGT1_VGT2_CONTR_B020020611.H0V0.MIR", 
	"VGT1_VGT2_CONTR_B020030401.H0V0.MIR", 
	"VGT1_VGT2_CONTR_B020030711.H0V0.MIR", 
	"VGT1_VGT2_CONTR_B020031211.H0V0.MIR" )

( tmp1 <- sapply( strsplit(x, split="_"), tail, n=1 ) )
[1] "B020020301.H0V0.MIR" "B020020611.H0V0.MIR" "B020030401.H0V0.MIR"
[4] "B020030711.H0V0.MIR" "B020031211.H0V0.MIR"

( tmp2 <- sapply( strsplit( tmp1, split="\\." ), head, n=1 ) )
[1] "B020020301" "B020020611" "B020030401" "B020030711" "B020031211"

gsub( "B", "", tmp2 )
[1] "020020301" "020020611" "020030401" "020030711" "020031211"
 

Here is another approach using regular expressions. Please be careful
with my one-liner as I am no expert in this. Maybe someone on the list
can point if there are any errors 
	gsub(".*\_B?(.*)\\.H.*", "\\1", x)


If you want reliability, go with the first suggestion.

Regards, Adai



On Tue, 2005-03-01 at 13:36 +0100, Jonathan Charrier wrote:
> hi everybody,
> 
> i try to extract  a part of name to a date :
> 
> like : "VGT1_VGT2_CONTR_B020030401.H0V0.MIR"    to    "20030401"
> 
> but the beginning of the files changes
> 
> i have a list of files:
> 
>  [,1]                                
> [1,] "VGT1_CONTR_B020020301.H0V0.MIR"                                 
> [2,] "VGT1_VGT2_CONTR_B020020611.H0V0.MIR"
> [3,] "VGT1_VGT2_CONTR_B020030401.H0V0.MIR"
> [4,] "VGT1_VGT2_CONTR_B020030711.H0V0.MIR"
> [5,] "VGT1_VGT2_CONTR_B020031211.H0V0.MIR"
> 
> i try the function "get()" and  "assign()" but without succeed
> 
> i know it's easy in C but in R i don't know the way
> 
> 
> if you have an idee
> 
> 
> thanks ,
> 
> Jonathan Charrier
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list