Re: [R] UNIX-like "cut" command in R

From: Mike Miller <mbmiller+l_at_gmail.com>
Date: Mon, 02 May 2011 21:32:03 -0500

On Tue, 3 May 2011, Andrew Robinson wrote:

> try substr()

OK. Apparently, it allows things like this...

> substr("abcdef",2,4)
[1] "bcd"

...which is like this:

echo "abcdef" | cut -c2-4

But that doesn't use a delimiter, it only does character-based cutting, and it is very limited. With "cut -c" I can do stuff this:

echo "abcdefghijklmnopqrstuvwxyz" | cut -c-3,12-15,17-

abclmnoqrstuvwxyz

It extracts characters 1 to 3, 12 to 15 and 17 to the end.

That was a great tip, though, because it led me to strsplit, which can do what I want, however somewhat awkwardly:

> y <- "a b c d e f g h i j k l m n o p q r s t u v w x y z"
> paste(unlist(strsplit(y, delim))[c(1:3,12:15,17:26)], collapse=delim)
[1] "a b c l m n o q r s t u v w x y z"

That gives me what I want, but it is still a little awkward. I guess I don't quite get what I'm doing with lists. I'm not clear on how this would work with a vector of strings.

Mike



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. Received on Thu 05 May 2011 - 06:25:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 05 May 2011 - 07:00:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive