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

  • Home
  • SEARCH
  • 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 8772191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:58:49+00:00 2026-06-13T17:58:49+00:00

Possible Duplicate: R: How to convert string to variable name? In R, I’m writing

  • 0

Possible Duplicate:
R: How to convert string to variable name?

In R, I’m writing a for-loop that will iteratively create variable names and then assign values to each variable.

Here is a simplified version. The intention is to create the variable’s name based on the value of iterating variable i, then fill the new variable with NA values.

(I’m only iterating 1:1 below since the problem occurs isn’t related to the looping itself, but rather to the way the variable is being created and assigned.)

for (i in 1:1) {

    #name variable i "Variablei"
    varName = paste("Variable", as.character(i), sep="")

    #fill variable with NA values
    varName = rep(NA, 12)

    print(varName)
    print(Variable1)
}

Now, varName prints out as

 [1] NA NA NA NA NA NA NA NA NA NA NA NA

and Variable1 is not found.

I understand on some level why this is buggy. In the first line, varName becomes a vector whose only entry is the string “Variable1”. Then varName gets reassigned to hold the NA values. So when I try to print Variable1, it doesn’t exist.

I think the more general issue is assignment vs. equality. In the first line, I want varName to be equal to the newly made string, but in the next line, I want varName to be assigned to the NA value vector.

What is the simplest way of creating that distinction? I’m also open to entirely different, better ways to go about this.

EDIT: Changed title because I had mischaracterized the problem.

  • 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-06-13T17:58:50+00:00Added an answer on June 13, 2026 at 5:58 pm

    Assignment works like this:

    <varname> = <expression>
    

    or more traditionally

    <varname> <- <expression>
    

    So, in your code, you have only ever assigned to varName. It’s not about assignment vs equality, just assignment. You may want to look at assign:

    for (i in 1:5) {
      assign(paste0("Variable", i), 10*i)
    } 
    

    as a toy example.

    Moreover, as noted in the comments, there are probably better approaches for your application. For example, why not just use a vector myvector and instead of having variables called Variable1, Variable2, etc you can refer to myvector[1], myvector[2] etc.

    As an example, let us say you had planned to work with

    Variable1 <- 'foo'
    Variable2 <- 'bar'
    Variable3 <- 'baz'
    

    then, you could change you approach, and set

    mydata <- c('foo', 'bar', 'baz')
    

    and where you would previously have used Variable2 (which contains 'bar') you instead use mydata[2] (which also contains 'bar'). The point here is that it is much easier to work with vectors and dataframes in R than a long list of variables.

    You could go further and name the entries:

    names(mydata) <- paste0("V", 1:3)
    

    which then allows you to write mydata["V2"] to retrieve bar.

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

Sidebar

Related Questions

Possible Duplicate: How to convert a time to a string I have a variable
Possible Duplicate: How to convert string as object's field name in javascript I can
Possible Duplicate: Java - Convert String to enum I have a method that uses
Possible Duplicate: Convert String to code I have a program that needs to be
Possible Duplicate: Convert a number to the shortest possible character string while retaining uniqueness
Possible Duplicate: How can I convert String to Int? public List<int> GetListIntKey(int keys) {
Possible Duplicate: How can I convert a string to boolean in JavaScript? I have
Possible Duplicate: C++ std::string conversion problem on Windows How to convert std::string to LPCSTR?
Possible Duplicate: Alternative to itoa() for converting integer to string C++? How to convert
Possible Duplicate: convert string to 2D array using php I have the string like

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.