Skip to content
Prev 388479 / 398513 Next

problem for strsplit function

On Thu, 8 Jul 2021 01:27:48 +0000 (UTC)
Kai Yang via R-help <r-help at r-project.org> wrote:

            
Your example is confusing/garbled.  You are applying strsplit() to a
single character string namely "name1.csv".  Your intent presumably is
to apply it to a *vector* (say "v") of file names.

A syntax which would work is

   sapply(strsplit(v,"\\."),function(x){x[1]})

E.g.

    v <- c("clyde.txt","irving.tex","melvin.pdf","fred.csv")
    sapply(strsplit(v,"\\."),function(x){x[1]})

which gives the output
Note that the output of strplit() is a *list* the i-th entry of which
is a vector consisting of the "split" of the i-th entry of the vector
to which strsplit() is applied.  In your example (corrected, so that it
makes sense, by replacing the string "names.csv" by my vector "v") you
get

    strsplit(v,"\\.")[[1]]
    [1] "clyde" "txt" 

the result of splitting "clyde.txt"; you want the first entry of this
result, i.e. "clyde".

My "sapply()" construction produces the first entry of each entry of the
list produced by strsplit().

It is useful to get your thoughts clear, understand what you are doing
and understand what the functions that you are using do.  (Read the
help!)

cheers,

Rolf Turner