[R] problem for strsplit function

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Thu Jul 8 04:49:10 CEST 2021


You would need to loop through the list to use strsplit() -- you are
confused about list structure.

Here's a simple way to do it using regex's -- **assuming that there is
only one period in your names that delineates the extension.** If this
is not true, then this **will fail**. This is vectorized and so will
be more efficient than looping.

d <- data.frame (fn = c("name1.csv", "name2.txt"))
d
d$first <- sub("\\..+","",d$fn)
d

Cheers,
Bert

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Wed, Jul 7, 2021 at 6:28 PM Kai Yang via R-help <r-help using r-project.org> wrote:
>
>  Hello List,
> I have a  one column data frame to store file name with extension. I want to create new column to keep file name only without extension.
> I tried to use strsplit("name1.csv", "\\.")[[1]] to do that, but it just retain the first row only and it is a vector.  how can do this for all of rows and put it into a new column?
> thank you,
> Kai
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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