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

Romain Francois romain.francois at dbmail.com
Fri Jul 3 14:22:00 CEST 2009


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




More information about the R-help mailing list