Re: [R] problem for strsplit function

2021-07-10 Thread Robert Knight
My method would be to use parse and deparse and substitute. It would iterate over each file name and build a new list of file names with the last four characters removed to have only the left side, and only the last four remaining to have only the right side. Then a new dataframe would be crea

Re: [R] problem for strsplit function

2021-07-09 Thread Rolf Turner
This discussion has developed in such a way that it seems a better subject line would be "problem for the hairsplit function". :-) cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276

Re: [R] problem for strsplit function

2021-07-09 Thread Jeff Newmiller
A bit too fast there, Duncan... x[[c(1,2)]] is illegal. On July 9, 2021 5:16:13 PM PDT, Duncan Murdoch wrote: >On 09/07/2021 6:44 p.m., Bert Gunter wrote: >> OK, I stand somewhat chastised. >> >> But my point still is that what you get when you "extract" depends on >> how you define "extract." D

Re: [R] problem for strsplit function

2021-07-09 Thread Jeff Newmiller
My mental model for the `[` vs `[[` behavior is that `[` indexes multiple results while `[[` indexes only one item. If returning multiple items from a list the result must be a list. For consistency, `[` always returns a list when applied to a list. The double bracket drops the containing list.

Re: [R] problem for strsplit function

2021-07-09 Thread Duncan Murdoch
On 09/07/2021 6:44 p.m., Bert Gunter wrote: OK, I stand somewhat chastised. But my point still is that what you get when you "extract" depends on how you define "extract." Do note that ?"[" yields a help file titled "Extract or Replace Parts of an object"; and afaics, the term "subset" is not ex

Re: [R] problem for strsplit function

2021-07-09 Thread Bert Gunter
"But it takes me a while to get familiar R." Of course. That is true for all of us. Just keep on plugging away and you'll get it. Probably far better than I before too long. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (

Re: [R] problem for strsplit function

2021-07-09 Thread Kai Yang via R-help
Thanks Bert, I'm reading some books now. But it takes me a while to get familiar R. Best, KaiOn Friday, July 9, 2021, 03:06:11 PM PDT, Duncan Murdoch wrote: On 09/07/2021 5:51 p.m., Jeff Newmiller wrote: > "Strictly speaking", Greg is correct, Bert. > > https://cran.r-project.org/doc/

Re: [R] problem for strsplit function

2021-07-09 Thread Bert Gunter
OK, I stand somewhat chastised. But my point still is that what you get when you "extract" depends on how you define "extract." Do note that ?"[" yields a help file titled "Extract or Replace Parts of an object"; and afaics, the term "subset" is not explicitly used as Duncan prefers. The relevant

Re: [R] problem for strsplit function

2021-07-09 Thread Duncan Murdoch
On 09/07/2021 5:51 p.m., Jeff Newmiller wrote: "Strictly speaking", Greg is correct, Bert. https://cran.r-project.org/doc/manuals/r-release/R-lang.html#List-objects Lists in R are vectors. What we colloquially refer to as "vectors" are more precisely referred to as "atomic vectors". And withou

Re: [R] problem for strsplit function

2021-07-09 Thread Jeff Newmiller
"Strictly speaking", Greg is correct, Bert. https://cran.r-project.org/doc/manuals/r-release/R-lang.html#List-objects Lists in R are vectors. What we colloquially refer to as "vectors" are more precisely referred to as "atomic vectors". And without a doubt, this "vector" nature of lists is a ke

Re: [R] problem for strsplit function

2021-07-09 Thread Bert Gunter
"1. a column, when extracted from a data frame, *is* a vector." Strictly speaking, this is false; it depends on exactly what is meant by "extracted." e.g.: > d <- data.frame(col1 = 1:3, col2 = letters[1:3]) > v1 <- d[,2] ## a vector > v2 <- d[[2]] ## the same, i.e > identical(v1,v2) [1] TRUE > v3

Re: [R] problem for strsplit function

2021-07-09 Thread Greg Minshall
Kai, > one more question, how can I know if the function is for column > manipulations or for vector? i still stumble around R code. but, i'd say the following (and look forward to being corrected! :): 1. a column, when extracted from a data frame, *is* a vector. 2. maybe your question is "i

Re: [R] problem for strsplit function

2021-07-08 Thread Kai Yang via R-help
Hello all, I have to learning R from beginning, since my group will get rid of SAS. So, my question may not be very clear for professional R user. I always dealing with column in data frame, not data vector.  Many thanks to Greg's example. it is very helpful. one more question, how can I know if

Re: [R] problem for strsplit function

2021-07-07 Thread Jeff Newmiller
I trust the escapes to do what they are designed to do. Cat the pattern to the console if you don't. On July 7, 2021 10:36:43 PM PDT, Greg Minshall wrote: >> sub( "\\.[^.]*$", "", fname ) > >fwiw, i almost always use '[.]' in preference to '.', as it >seems to be more likely to get throu

Re: [R] problem for strsplit function

2021-07-07 Thread Greg Minshall
> sub( "\\.[^.]*$", "", fname ) fwiw, i almost always use '[.]' in preference to '.', as it seems to be more likely to get through the various levels of quoting in different contexts. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and mo

Re: [R] problem for strsplit function

2021-07-07 Thread Jeff Newmiller
sub( "\\.[^.]*$", "", fname ) On July 7, 2021 6:27:48 PM PDT, Kai Yang via R-help 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 d

Re: [R] problem for strsplit function

2021-07-07 Thread Rolf Turner
On Thu, 8 Jul 2021 01:27:48 + (UTC) Kai Yang via R-help 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 > j

Re: [R] problem for strsplit function

2021-07-07 Thread Bert Gunter
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

Re: [R] problem for strsplit function

2021-07-07 Thread Andrew Simmons
Hello, I would suggest something like `tools::file_path_sans_ext` instead of `strsplit` to remove the file extension. This is also vectorized, so you won't have to use a `sapply` or `vapply` on it. I hope this helps! On Wed, Jul 7, 2021 at 9:28 PM Kai Yang via R-help wrote: > Hello List, > I

[R] problem for strsplit function

2021-07-07 Thread Kai Yang via R-help
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