Hi Monica,
Try
sapply(as.list(str),function(x) unlist(strsplit(x,"
"))[1])
--- Monica Pisica <[EMAIL PROTECTED]> wrote:
>
> Hi everyone,
>
> I have a vector of strings, each string made up by
> different number of words. I want to get a new
> vector which has only the first word of each str
> Subject: Re: [R] spliting strings
...> > Monica Pisica wrote:> > Hi everyone,> > > > I have a vector of strings,
each string made up by different number of words. I want to get a new vector
which has only the first word of each string in the first vector. I came up
with this
On Thu, 13 Dec 2007, Monica Pisica wrote:
>
> Hi everyone,
>
> I have a vector of strings, each string made up by different number of
> words.
You need to define 'word' and 'first'. Your solution says the first word
of " aa" is "", which is not what most people would think.
> I want to get a
Good afternoon Monica,
Relying on regular expressions, substituting nothing "" for everything
starting with a space until the end of the "line" (i.e. with a dollar sign)
str1 <- sub(" .*$", "", str)
Regards,
Sean
Monica Pisica wrote:
>
>
> Hi everyone,
>
> I have a vector of strings, eac
Monica Pisica wrote:
> Hi everyone,
>
> I have a vector of strings, each string made up by different number of words.
> I want to get a new vector which has only the first word of each string in
> the first vector. I came up with this:
>
> str <- c('aaa bbb', 'cc', 'd eee aa', 'mmm o n')
> st
;
To: <[EMAIL PROTECTED]>
Sent: Thursday, December 13, 2007 2:41 PM
Subject: [R] spliting strings ...
>
> Hi everyone,
>
> I have a vector of strings, each string made up by different number
> of words. I want to get a new vector which has only the first word
> of e
Hi,
if you only want the first word, then this should do the trick :
R> sub( " +.*", "", str )
[1] "aaa" "cc" "d" "mmm"
Cheers,
Romain
Monica Pisica wrote:
> Hi everyone,
>
> I have a vector of strings, each string made up by different number of words.
> I want to get a new vector which
Hi everyone,
I have a vector of strings, each string made up by different number of words. I
want to get a new vector which has only the first word of each string in the
first vector. I came up with this:
str <- c('aaa bbb', 'cc', 'd eee aa', 'mmm o n')
str1 <- rep(1, length(str))
for (i in
8 matches
Mail list logo