[R] UNIX-like "cut" command in R
Mike Miller
mbmiller+l at gmail.com
Tue May 3 08:39:49 CEST 2011
On Tue, 3 May 2011, Christian Schulz wrote:
>> On Mon, 2 May 2011, P Ehlers wrote:
>>
>>> Use str_sub() in the stringr package:
>>>
>>> require(stringr) # install first if necessary
>>> s <- "abcdefghijklmnopqrstuvwxyz"
>>>
>>> str_sub(s, c(1,12,17), c(3,15,-1))
>>> #[1] "abc" "lmno" "qrstuvwxyz"
>>
>>
>> Thanks. That's very close to what I'm looking for, but it seems to
>> correspond to "cut -c", not to "cut -f". Can it work with delimiters or
>> only with character counts?
>>
>> Mike
>>
>
> x <- "this is a string"
> unlist(strsplit(x," "))[c(1,4)]
Thanks. I did figure that one out a couple of messages back, but to get
it do behave like "cut -d' ' -f1,4", I had to add a paste command to
reassemble the parts:
paste(unlist(strsplit(x," "))[c(1,4)], collapse=" ")
Then I wasn't sure if I could do this to every element of a vector of
strings without looping -- I have to think not.
Mike
More information about the R-help
mailing list