[R] Remove all spaces from a string so it can be used by assign()

John Kane jrkrideau at yahoo.ca
Mon Jul 6 16:25:01 CEST 2009


instrument.input[,6] appears to be a vector of factors.  You are putting the underlying factor number into data. Assuming instrument.input[,6]  is supposed to be numeric you will need to convert from factor. 

See the FAQ Part 7 R Miscellanea > How do I convert factors to numeric?
for that.

--- On Fri, 7/3/09, Kurt Smith <kurt.smith at hotmail.co.uk> wrote:

> From: Kurt Smith <kurt.smith at hotmail.co.uk>
> Subject: Re: [R] Remove all spaces from a string so it can be used by assign()
> To: romain.francois at dbmail.com
> Cc: r-help at r-project.org
> Received: Friday, July 3, 2009, 8:34 AM
> 
> Thanks for the help, I really appreciate it.
> 
> However, I have one more small issue and I don't really
> want to post a second message in such a short space of time,
> so I will put it up here if someone is willing to help.
> 
> Basically I need to transfer two vectors into a
> data.frame/matrix (with a unique name, as already discussed
> above). However, the numbers that enter into the data.frame
> do not match the numbers that I supposedly entered from the
> vector. Example.
> 
> > instrument.input[3,6]
> [1] 769873.58
> 43 Levels: 10840277.06 109014288.37 11055630.67 11456522.47
> ... CPS
> > data <- instrument.input[3,6]
> > data
> [1] 769873.58
> 43 Levels: 10840277.06 109014288.37 11055630.67 11456522.47
> ... CPS
> 
> Works fine if I put it as a sole varible.. however if I try
> adding it to a data.frame/matrix
> 
> > temp.data1[1,1] <- instrument.input[3,6]
> > temp.data1[1,1]
> CPS 
>  31 
> 
> The incorrect number is given, if I've understood this
> properly I should be recalling 769873.
> 
> Any suggestions or thoughts?
> 
> 
> Cheers 
> 
> Kurt 
> 
> 
> 
> > Date: Fri, 3 Jul 2009 14:22:00 +0200
> > From: romain.francois at dbmail.com
> > To: kurt.smith at hotmail.co.uk
> > CC: R-help at r-project.org
> > Subject: Re: [R] Remove all spaces from a string so it
> can be used by assign()
> > 
> > On 07/03/2009 02:13 PM, Kurt Smith wrote:
> > > Are you sure it would work?
> > >
> > > It works when I physically enter "56 Fe [1]" but
> fails when I try to
> > > enter that anything other then directly.
> > >
> > >>  instrument.input[1,6]
> > > [1] 56  Fe  [ 1 ]
> > > 43 Levels: 10840277.06 109014288.37 11055630.67
> 11456522.47 ... CPS
> > >>  assign(instrument.input[1,6],
> temp.data)
> > > Error in assign(instrument.input[1, 6],
> temp.data) :
> > >    invalid first argument
> > 
> > that is because instrument.input[1,6] is a factor. You
> would need 
> > something like this untested call :
> > 
> > R> assign( as.character( instrument.input[1,6] ),
> temp.data )
> > 
> > Whether all of this is a good idea is up to you
> > 
> > >>  temp.name<- instrument.input[1,6]
> > >>  temp.name
> > > [1] 56  Fe  [ 1 ]
> > > 43 Levels: 10840277.06 109014288.37 11055630.67
> 11456522.47 ... CPS
> > >>  assign(temp.name, temp.data)
> > > Error in assign(temp.name, temp.data) : invalid
> first argument
> > >
> > > and it seems that it is because the varible
> begins with an integer R doesn't like it.
> > > Would it be possible to use gsub to create
> Fe561?
> > 
> > sure.
> > 
> > R> gsub( "[^[:alnum:]]", "", gsub(
> "([[:digit:]]+)(.*)", "\\2\\1", "56 
> > Fe [1]" ) )
> > [1] "Fe156"
> > 
> > others probably have a one call solution
> > 
> > >>  R>  gsub( "[^[:alnum:]]", "",
> "56 Fe [1]" )
> > >>  [1] "56Fe1"
> > >
> > >
> > >
> > >  > Date: Fri, 3 Jul 2009 13:09:20 +0200
> > >  > From: romain.francois at dbmail.com
> > >  > To: kurt.smith at hotmail.co.uk
> > >  > CC: r-help at r-project.org
> > >  > Subject: Re: [R] Remove all spaces
> from a string so it can be used by
> > > assign()
> > >  >
> > >  > On 07/03/2009 12:56 PM, Kurt Smith
> wrote:
> > >  > >
> > >  > > Hi
> > >  > >
> > >  > > I have a string "56 Fe [1]" that
> I would like to use as a variable
> > > name by using "assign" however I think the spaces
> and brackets might be
> > > causing R some trouble.
> > >  >
> > >  > Not really;
> > >  >
> > >  > R> assign( "56 Fe [1]", 10 )
> > >  > R> `56 Fe [1]`
> > >  > [1] 10
> > >  >
> > >  > > How can I change the string so
> that it just becomes 56Fe1 and can
> > > be used as a variable name.
> > >  >
> > >  > 56Fe1 is not good enough to be
> assigned without quotes
> > >  >
> > >  > R> 56Fe1 <- 10
> > >  > Error: unexpected symbol in "56Fe1"
> > >  >
> > >  > You might be interested in make.names
> > >  >
> > >  > R> make.names( "56 Fe [1]" )
> > >  > [1] "X56.Fe..1."
> > >  >
> > >  > Otherwise, to answer your question,
> you can :
> > >  >
> > >  > R> gsub( "[^[:alnum:]]", "", "56 Fe
> [1]" )
> > >  > [1] "56Fe1"
> > >  >
> > >  >
> > >  > > Thank You
> > >  > >
> > >  > > Kurt
> > >  >
> > >  >
> > >  > --
> > >  > Romain Francois
> > >  > Independent R Consultant
> > >  > +33(0) 6 28 91 30 30
> > >  > http://romainfrancois.blog.free.fr
> > >  > |- http://tr.im/qJ8V : RGG#153: stars
> network
> > >  > |- http://tr.im/qzSl : using ImageJ from R:
> the RImageJ package
> > >  > `- http://tr.im/qzSJ : with semantics for
> java objects in rJava
> > >
> > >
> ------------------------------------------------------------------------
> > > View your Twitter and Flickr updates from one
> place - Learn more!
> > > <http://clk.atdmt.com/UKM/go/137984870/direct/01/>
> > 
> > 
> > -- 
> > Romain Francois
> > Independent R Consultant
> > +33(0) 6 28 91 30 30
> > http://romainfrancois.blog.free.fr
> > |- http://tr.im/qJ8V : RGG#153: stars network
> > |- http://tr.im/qzSl : using ImageJ from R: the RImageJ
> package
> > `- http://tr.im/qzSJ : with semantics for java objects in
> rJava
> 
> _________________________________________________________________
> 
> 
>     [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
> 


      __________________________________________________________________
Looking for the perfect gift? Give the gift of Flickr! 

http://www.flickr.com/gift/




More information about the R-help mailing list