Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6903285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:54:24+00:00 2026-05-27T07:54:24+00:00

Several months ago I asked something similar , but I was using JavaScript to

  • 0

Several months ago I asked something similar, but I was using JavaScript to check if provided string is a “valid” R object name. Now I’d like to achieve the same by using nothing but R. I suppose that there’s a very nice way to do this, with some neat (not so) esoteric R function, so regular expressions seem to me as the last line of defence. Any ideas?

Oh, yeah, using back-ticks and stuff is considered cheating. =)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:54:24+00:00Added an answer on May 27, 2026 at 7:54 am

    Edited 2013-1-9 to fix regular expression. Previous regular expression, lifted from page 456 of John Chambers’ "Software for Data Analysis", was (subtly) incomplete. (h.t. Hadley Wickham)


    There are a couple of issues here. A simple regular expression can be used to identify all syntactically valid names — but some of those names (like if and while) are ‘reserved’, and cannot be assigned to.

    • Identifying syntactically valid names:

      ?make.names explains that a syntactically valid name:

      […] consists of letters, numbers and the
      dot or underline characters and starts with a letter or the dot
      not followed by a number. Names such as ‘".2way"’ are not valid […]

      Here is the corresponding regular expression:

       "^((([[:alpha:]]|[.][._[:alpha:]])[._[:alnum:]]*)|[.])$"
      
    • Identifying unreserved syntactically valid names

      To identify unreserved names, you can take advantage of the base function make.names(), which constructs syntactically valid names from arbitrary character strings.

      isValidAndUnreserved <- function(string) {
          make.names(string) == string
      }
      
      isValidAndUnreserved(".jjj")
      # [1] TRUE
      isValidAndUnreserved(" jjj")
      # [1] FALSE
      
    • Putting it all together

      isValidName <- function(string) {
          grepl("^((([[:alpha:]]|[.][._[:alpha:]])[._[:alnum:]]*)|[.])$", string)
      }
      
      isValidAndUnreservedName <- function(string) {
          make.names(string) == string
      }
      
      testValidity <- function(string) {
          valid <- isValidName(string)
          unreserved <- isValidAndUnreservedName(string)
          reserved <- (valid & ! unreserved)
          list("Valid"=valid,
               "Unreserved"=unreserved,
               "Reserved"=reserved)
      }
      
      testNames <- c("mean", ".j_j", ".", "...", "if", "while", "TRUE", "NULL",
                     "_jj", "  j", ".2way") 
      t(sapply(testNames, testValidity))
      
            Valid Unreserved Reserved
      mean  TRUE  TRUE       FALSE   
      .j_j  TRUE  TRUE       FALSE
      .     TRUE  TRUE       FALSE     
      ...   TRUE  TRUE       FALSE   
      if    TRUE  FALSE      TRUE    
      while TRUE  FALSE      TRUE    
      TRUE  TRUE  FALSE      TRUE    
      NULL  TRUE  FALSE      TRUE    
      _jj   FALSE FALSE      FALSE   
        j   FALSE FALSE      FALSE   # Note: these tests are for "  j", not "j"
      .2way FALSE FALSE      FALSE
      

    For more discussion of these issues, see the r-devel thread linked to by @Hadley in the comments below.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've started to use XCode several months ago, after using IntelliJ for several years,
I downloaded and installed Eclipse using Yoxos a few months ago. Since then, several
I started using Dist::Zilla several months ago. However, at YAPC::NA someone mentioned that they
Several months ago, I installed ruby 1.9.1 on Mac OSX 10.6 using the instructions
Several months ago for a project at work i developed a video player using
I believe this question was asked several months back, but i believe my situation
Several months ago I learned from an answer on Stack Overflow how to perform
I wrote a small PHP application several months ago that uses the WordPress XMLRPC
I built a Winform app several months ago that schedules appointments for repair techs.
I've switch from VB.NET to C# several months ago, while there are a lot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.