Little Helpers: Character index counter
[This article was first published on Rcrastinate, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I find myself counting characters on my screen more often than I feel comfortable doing it. Isn’t it weird to count things by hand when you have the power of R right in front of you? Well, I decided to but a little helper function into my .Rprofile
file (i.e. the function is defined with every startup of R and available throughout the session) that cares care of that (and is less error-prone at the same time).
.charind <- function (char) { lapply(char, FUN = function (x) { spl <- strsplit(x, "")[[1]] ret <- 1:length(spl) names(ret) <- spl ret }) }
By the way: Whenever the name of an R object starts with a dot .
this object is invisible - so, this little helper will not clutter your RStudio “Environment” pane. Let’s test it:
.charind(c("2020-12-26", "Klärschlammentsorgungsrichtlinie")) ## [[1]] ## 2 0 2 0 - 1 2 - 2 6 ## 1 2 3 4 5 6 7 8 9 10 ## ## [[2]] ## K l ä r s c h l a m m e n t s o r g u n g s r i c h ## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ## t l i n i e ## 27 28 29 30 31 32
No counting with fingers on the screen anymore…
To leave a comment for the author, please follow the link and comment on their blog: Rcrastinate.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.