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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:56:40+00:00 2026-06-13T12:56:40+00:00

And when I initialize the new array, would I need every value in this

  • 0

And when I initialize the new array, would I need every value in this field to be initialized with an actual numerical value?

  • 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-13T12:56:41+00:00Added an answer on June 13, 2026 at 12:56 pm

    How you append the new field will depend on the object you have. Options are given below. If the length() of the variable you are trying to append does not equal the number of rows of the object you wish to append to then you will need to fill the new column/variable first with NA and then append your variable into the correct elements of the newly created object. For example, with

    df <- data.frame(var1 = 1:5, var2 = LETTERS[1:5])
    

    and we want to add a vector c(1,3,4) into rows 1, 2, and 5 of a third variable in df, then we could do this like so:

    df$var3 <- NA ## note the single NA is recycled to the correct length
    

    Then using subsetting to insert the new data into the correct elements

    df$var3[c(1,2,5)] <- c(1,3,4)
    

    which gives:

    > df$var3[c(1,2,5)] <- c(1,3,4)
    > df
      var1 var2 var3
    1    1    A    1
    2    2    B    3
    3    3    C   NA
    4    4    D   NA
    5    5    E    4
    

    If the variable you wish to append is of the correct length, then you don’t need to initialise anything. Use one of the approaches described below to achieve what you want in that case, depending on whether you have a data frame or a matrix.

    Assuming a data frame

    If you have a data frame then there are several ways. For example, using

    df <- data.frame(var1 = 1:10, var2 = LETTERS[1:10])
    

    We can do

    > df$var3 <- letters[1:10]
    > df
       var1 var2 var3
    1     1    A    a
    2     2    B    b
    3     3    C    c
    4     4    D    d
    5     5    E    e
    6     6    F    f
    7     7    G    g
    8     8    H    h
    9     9    I    i
    10   10    J    j
    

    I prefer transform()

    df <- transform(df, var3 = letters[1:10])
    

    or the related within()

    df <- within(df, var3 <- letters[1:10]) ## note diff syntax
    

    and you can also do

    df[, "var3"] <- letters[1:10]
    

    or

    df[, 3] <- letters[1:10]
    

    but the latter one doesn’t produce a variable with the name "var3" but instead gets a default name.

    You can also use cbind() to bind a column to a data frame (also works for matrices). Note that you have to either pass a data frame as the first argument or call the "data.frame" method directly otherwise you don’t get a data frame as the result:

    df <- cbind(df, var3 = letters[1:10])
    df2 <- cbind.data.frame(var3 = letters[1:10], df)
    ## note `df2` will have `var3` as the first column here
    

    Assuming a matrix

    If you have a matrix, then the options are reduced essentially to using cbind(). Using

    mat <- matrix(1:20, ncol = 2)
    

    you can do

    mat <- cbind(mat, 1:10)
    

    The other options don’t work.

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

Sidebar

Related Questions

You have: val array = new Array[Array[Cell]](height, width) How do you initialize all elements
I initialized an Array as Double[][] myarr = Enumerable.Repeat(new double[12], 13).ToArray(); Then in a
I am new to Knockout.js, I have created an observable array and initialized with
I found out this morning that proc.new works in a class initialize method, but
Why is it possible to initialize a Dictionary<T1,T2> like this: var dict = new
I have a System.Array of value struct types, something like this: public value struct
I need to return an array of initialized objects from VB6 into C# using
Objective Initialize a new addition problem for users to solve when they visit /task
I'm doing an INSERT INTO query in order to initialize a new table. The
Can I initialize an IOLoop.instance and than fork new processes, which uses IOLoop.instance()? Something

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.