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 8911935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:04:29+00:00 2026-06-15T04:04:29+00:00

I know pointer is not the right terminology, but I hope it is close

  • 0

I know “pointer” is not the right terminology, but I hope it is close enough. I have the following code:

> attach(iris)
> iris$x <-0
> x
Error: object 'x' not found

It seems that after attach(), R created one object for each variable. But after I create a new variable by direct access to the data set, there’s no new object created for that variable.

How should I create a new variable for iris if I want R to create a new object for it as well?

Update: I know many R veterans suggest not to use attach(). But I need to do heavy data manipulation (hundreds of new variables), and calling transform(df,) on everyone of them sequentially would be quite cumbersome. Is it possible in R to do similar to SAS datastep, where within one data step, you can create a variable and refer to it right after:

 data A;
 set A;
 varA = varB > 1;
 varC = var A + varB;
 ....
 run;

Update 2: One way I can think of is to use attach(), then create hundreds of arrays then cbind them before detach()
Thank you.

  • 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-15T04:04:30+00:00Added an answer on June 15, 2026 at 4:04 am

    attach() has done nothing of the sort; it has just placed a copy of the object on the search path which allows R to look inside the object when it is looking up names.

    The key word above is “copy”. Which explains the behaviour you see; the copy of iris on the search path is not the one in the global workspace and you are modifying the latter not the former.

    Simple answer is don’t use attach(). For adding variables to objects, look at transform() and within().

    iris <- transform(iris, x = rep(0, nrow(iris)))
    

    or

    iris <- within(iris, x <- rep(0, nrow(iris)))
    

    To use components within objects directly, you can use with(), e.g.

    with(iris, length(x))
    

    I suggest you read the relevant help files for examples of usage etc.

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

Sidebar

Related Questions

I do not know why but I have problems with this code. The banner
I know I asked these question before but I did not get the right
This is not exactly a straight-out question because I have just solved it, but
Simple question, I just need a pointer in the right direction: I have a
I know I'm not asking this quite right, either. Please help me better form
So I know as3 doesn't have pointers, but I thought there might be a
My code is ok but unfortunately my code gives me null pointer exception. I
Sounds simple enough, but its not working. In this example, I want to set
I would like to know standard's view on dereferencing pointer to base, but I'm
I have some experience in Python and Java, but I dont really know the

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.