Kurt Smith
2009-Jul-03 10:56 UTC
[R] Remove all spaces from a string so it can be used by assign()
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. How can I change the string so that it just becomes 56Fe1 and can be used as a variable name. Thank You Kurt _________________________________________________________________ [[elided Hotmail spam]] [[alternative HTML version deleted]]
Romain Francois
2009-Jul-03 11:09 UTC
[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
Romain Francois
2009-Jul-03 12:22 UTC
[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 argumentthat 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
Greg Snow
2009-Jul-04 01:59 UTC
[R] Remove all spaces from a string so it can be used by assign()
The only people who should use the assign function are those who fully understand why you should never use the assign function. If you tell us where the string is coming from and what you are trying to accomplish, then maybe we can help you accomplish it in a better way. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Kurt Smith > Sent: Friday, July 03, 2009 4:57 AM > To: r-help at r-project.org > Subject: [R] Remove all spaces from a string so it can be used by > assign() > > > 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. > > How can I change the string so that it just becomes 56Fe1 and can be > used as a variable name. > > > Thank You > > Kurt > > _________________________________________________________________ > > [[elided Hotmail spam]] > > > [[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.
Jason Rupert
2009-Jul-04 03:19 UTC
[R] Remove all spaces from a string so it can be used by assign()
Don't think you can have numerics as the first characters in a variable name. R ninjas can confirm, but I think you will be able to have numerics after an initial alpha character, e.g. Fe56 is ok, but 56Fe is not ok. Also, check out "gsub". This is a really powerful string editing tool. Hope this helps.> > > -----Original Message----- > > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r- > > project.org] On Behalf Of Kurt Smith > > Sent: Friday, July 03, 2009 4:57 AM > > To: r-help at r-project.org > > Subject: [R] Remove all spaces from a string so it can > be used by > > assign() > > > > > > 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. > > > > How can I change the string so that it just becomes > 56Fe1 and can be > > used as a variable name. > > > > > > Thank You > > > > Kurt > > > > > _________________________________________________________________ > > > > [[elided Hotmail spam]] > > > > > > ??? [[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. > > ______________________________________________ > 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. >
John Kane
2009-Jul-06 14:25 UTC
[R] Remove all spaces from a string so it can be used by assign()
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/
Bert Gunter
2009-Jul-06 15:35 UTC
[R] Remove all spaces from a string so it can be used by assign() -- A Fortune?
The below seems like a Fortunes candidate to me. -- Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Greg Snow Sent: Friday, July 03, 2009 7:00 PM To: Kurt Smith; r-help at r-project.org Subject: Re: [R] Remove all spaces from a string so it can be used by assign() The only people who should use the assign function are those who fully understand why you should never use the assign function. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111