I have a comma-separated data file in which trailing commas sometimes occur. I am using strsplit to extract the data from this file, and it seems great except in cases with trailing comma characters. The example below illustrates. What I'd like is to get a fourth element in the answer, being an empty string just like the second element. Is there a way I can express my patter (or perhaps specify perl or extended) to get that?> strsplit("a,,b,", ",")[[1]][1] "a" "" "b" -- View this message in context: http://www.nabble.com/strsplit-on-comma%2C-with-a-trailing-comma-in-input-tf4940023.html#a14141151 Sent from the R help mailing list archive at Nabble.com.
Benilton Carvalho
2007-Dec-03 23:33 UTC
[R] strsplit on comma, with a trailing comma in input
my understanding is that this behavior is known (the help file
mentions something along these lines in the example).
i'd use something like:
theText <- "a,,b,"
theText <- gsub("\\,$", ", ", theText)
and then use strsplit() on "theText"
b
On Dec 3, 2007, at 6:22 PM, dankelley wrote:
>
> I have a comma-separated data file in which trailing commas
> sometimes occur.
> I am using strsplit to extract the data from this file, and it seems
> great
> except in cases with trailing comma characters.
>
> The example below illustrates. What I'd like is to get a fourth
> element in
> the answer, being an empty string just like the second element. Is
> there a
> way I can express my patter (or perhaps specify perl or extended) to
> get
> that?
>
>
>> strsplit("a,,b,", ",")[[1]]
> [1] "a" "" "b"
>
>
>
> --
> View this message in context:
http://www.nabble.com/strsplit-on-comma%2C-with-a-trailing-comma-in-input-tf4940023.html#a14141151
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
Gabor Grothendieck
2007-Dec-03 23:33 UTC
[R] strsplit on comma, with a trailing comma in input
Try appending another comma:
strsplit(paste("a,,b,", ",", sep = ""),
",")
On Dec 3, 2007 6:22 PM, dankelley <Dan.Kelley at dal.ca>
wrote:>
> I have a comma-separated data file in which trailing commas sometimes
occur.
> I am using strsplit to extract the data from this file, and it seems great
> except in cases with trailing comma characters.
>
> The example below illustrates. What I'd like is to get a fourth
element in
> the answer, being an empty string just like the second element. Is there a
> way I can express my patter (or perhaps specify perl or extended) to get
> that?
>
>
> > strsplit("a,,b,", ",")[[1]]
> [1] "a" "" "b"
>
>
>
> --
> View this message in context:
http://www.nabble.com/strsplit-on-comma%2C-with-a-trailing-comma-in-input-tf4940023.html#a14141151
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
Peter Dalgaard
2007-Dec-03 23:44 UTC
[R] strsplit on comma, with a trailing comma in input
dankelley wrote:> I have a comma-separated data file in which trailing commas sometimes occur. > I am using strsplit to extract the data from this file, and it seems great > except in cases with trailing comma characters. > > The example below illustrates. What I'd like is to get a fourth element in > the answer, being an empty string just like the second element. Is there a > way I can express my patter (or perhaps specify perl or extended) to get > that? > > > >> strsplit("a,,b,", ",")[[1]] >> > [1] "a" "" "b" >Hmm, I don't think strsplit can do that. However: > scan(textConnection("a,,b,"), sep=",", what="") Read 4 items [1] "a" "" "b" "" -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907