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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:42:25+00:00 2026-06-12T04:42:25+00:00

Why is it that I can’t assign a value to an entire column of

  • 0

Why is it that I can’t assign a value to an entire column of a data frame, and then a single element in the same “within” statement? The code:

foo <- data.frame( a=seq(1,10) )

foo <- within(foo, {
  b <- 1  # set all of b to 1
})

foo <- within(foo, {
  c <- 1  # set all of c to 1
  c[2] <- 20  # set one element to 20
  b[2] <- 20
})

foo

Gives:

    a  b  c
1   1  1  1
2   2 20 20
3   3  1  1
4   4  1 20
5   5  1  1
6   6  1 20
7   7  1  1
8   8  1 20
9   9  1  1
10 10  1 20

The value of b is what I expected. The value of c is strange. It seems to do what I expect if the assignment to the entire column (ie b <- 1) is in a different “within” statement than the assignment to a single element (ie b[2] <- 20). But not if they’re in the same “within”.

Is this a bug, or something I just don’t understand about R?

  • 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-12T04:42:26+00:00Added an answer on June 12, 2026 at 4:42 am

    My guess is that the assignments to new columns are done as you “leave” the function. When doing

    c <- 1
    c[2] <- 20
    

    all you have really created is a vector c <- c(1, 20). When R has to assign this to a new column, the vector is recycled, creating the 1,20,1,20,… pattern you are seeing.

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

Sidebar

Related Questions

How can you query a column for any value in that column? (ie. How
Given that $struct is a data structure that can be either a hash or
How can I build a regex example that can escape single quote and backslash
I want to know that can we build a data object using jquery/json, which
There is a column that can have several values. I want to select a
How can I easily separate JSON values that are sent in the same request?
I have some data that can only be sorted in specific ways due to
I have a method that can return either a single object or a collection
I can do .one.two and that targets any element with the class one AND
how can i make a collection that can store a data type (say string,

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.