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

jim holtman jholtman at gmail.com
Sat Jul 4 06:47:12 CEST 2009


It all depends on what you want to do with the object.  If you use
back quotes, you can have the object named anything you want (I think
this was pointed out in a previous reply):

> `56Fe2` <- 1:10  # assign a value
> `56Fe2` * 3  # do an operation on it
 [1]  3  6  9 12 15 18 21 24 27 30

> get('56Fe2')  # get the object; notice the regular quotes in this case,
 [1]  1  2  3  4  5  6  7  8  9 10
> ls()  # list out the objects in my global environment (its there)
 [1] "56Fe2"             "allComm"           "colnames"
"comm"
 [5] "commMap"           "createVirtualQuad" "data"              "DF"

On Fri, Jul 3, 2009 at 11:19 PM, Jason Rupert<jasonkrupert at yahoo.com> wrote:
>
> 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.
>>
>
>
>
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list