[R] how to split a column by tab ?

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Mon Sep 9 20:16:36 CEST 2019


Hello,

I'm glad it helped.
One more thing. I forgot to set the column names, if you want to get 
them from the data, try something like this:

nms <- lapply(s, function(.s) .s[grepl("[[:alpha:]]+", .s)])
nms <- sapply(nms, paste, collapse = ".")
names(daT3) <- nms
daT3


Hope this helps,

Rui Barradas

Às 16:28 de 09/09/19, Marna Wagley escreveu:
> Thank you Rui. it helped a lot.
> Sincerely,
> MW
> 
> On Mon, Sep 9, 2019 at 6:47 AM Rui Barradas <ruipbarradas using sapo.pt 
> <mailto:ruipbarradas using sapo.pt>> wrote:
> 
>     Hello,
> 
>     You could try a character class instead of one character (the tab char).
>     There are two character classes that you can try, [:blank:] and
>     [:space:], see ?regex.
> 
>     strsplit(as.character(daT1), "[[:blank:]]+")
> 
>     Then form the final result. I have changed rbind to cbind, it seemed
>     more appropriate (?).
> 
>     s <- strsplit(as.character(daT1), "[[:blank:]]+")
>     x <- lapply(s, function(.s) .s[!grepl("[[:alpha:]]+", .s)])
>     daT3 <- as.data.frame(do.call(cbind, x))
>     daT3
> 
> 
>     Hope this helps,
> 
>     Rui Barradas
> 
>     Às 12:54 de 09/09/19, Marna Wagley escreveu:
>      > Hi R User,
>      > I was trying to split a column by tabs, I tried to split with
>     several ways,
>      > but I could not split it. Is there any possibilities?
>      >
>      > The data example and the code I used
>      > daT1<-c("Column number             1    2    3    4    5    6   
>     7    8
>      >   9   10   10   10   10   10   10   10",
>      > "comes from position       1    7    2    6    3    5   15    9 
>        4    8
>      > 10   11   12   13   14   16"
>      > )
>      > daT2 <- data.frame(do.call('rbind',
>      > strsplit(as.character(daT1),'\t',fixed=T)))
>      > colnames(daT2)
>      > daT2
>      >
>      >       [[alternative HTML version deleted]]
>      >
>      > ______________________________________________
>      > R-help using r-project.org <mailto: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.
>      >
> 
>     ______________________________________________
>     R-help using r-project.org <mailto: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