[R] splitting a string up

Marc Schwartz marc_schwartz at me.com
Fri Aug 21 22:16:43 CEST 2009


Just be aware that this only works if 'x' contains a single entry. If  
'x' is a vector of multiple such entries, you will only get the  
leading numbers in the first entry:

x2 <- c(x, x)

 > x2
[1] "1041281__2009_08_20_.lev" "1041281__2009_08_20_.lev"


 > strsplit(x2, split="_")[[1]][1]
[1] "1041281"


as compared to:


 > sapply(strsplit(x2, split = "_"), "[", 1)
[1] "1041281" "1041281"


HTH,

Marc Schwartz


On Aug 21, 2009, at 3:08 PM, Stephan Kolassa wrote:

> strsplit("1041281__2009_08_20_.lev", split="_")[[1]][1]
>
> HTH,
> Stephan
>
> stephen sefick schrieb:
>> x <- "1041281__2009_08_20_.lev"
>> I would like to split this string up and only extract the leading  
>> numbers.
>> 1041281
>> to use as a label for a data column in a bigger for loop function to
>> read in data.
>> regards,




More information about the R-help mailing list