Adding New Column To A Data Frame in R

That is a quite noob tip and me, as a noob in R, was pleased to do that. Let’s say you have a data.frame named “saida” with four columns “A”, “B”, “C”, “D”.

>saida # let's see the content of our data.frame "saida"

    A   B    C    D

1   1   64   20   0
2   2   64   20   3
3   3   64   20   3
4   4   64   20   1
5   5   64   20   4
>saida["MY_NEW_COLUMN"] <- NA # That creates the new column named "MY_NEW_COLUMN" filled with "NA"
>saida$MY_NEW_COLUMN <- saida$C - saida$D  # As an example, the new column receives the result of C - D
>saida # let's see the result

    A   B    C    D   MY_NEW_COLUMN

1   1   64   20   0   20
2   2   64   20   3   17
3   3   64   20   3   17
4   4   64   20   1   19
5   5   64   20   4   16

If you want to add many columns, one way to accomplish that is using a list with the names of the new columns, e.g.,:

saida[c("E","F","G","H","I")] <- NA # adds those many columns labeled from E to I.

I am sure there is a more elegant way to accomplish the same result but this is the only way that I know.

19 Responses to Adding New Column To A Data Frame in R

  1. Pingback: R: Add column to dataframe « Vikram's Book

  2. zach says:

    thanks! good post

  3. dylan says:

    nice one, thanks, R is doing my head in after coming from Octave and MATLAB!

  4. Hernan says:

    Thanks, this was useful!

  5. Elise says:

    Thanks!!! Beats trying to change every single one of my csv’s…

  6. Georgiana May says:

    Could you clarify what the 4:5 is in saida[4:5 ….
    Also, how do you add a whole column of numbers that are in another file (e.g. the output of another operation ‘predict’, predicting y-values for each x value given a model). the x values are in one column in the data frame, one wants to add the output of predict as another column.

    • Kel Murphy says:

      I ran it without the 4:5 and it worked for me.without error.
      myData[“Percentage”] <- NA; #create column in myData;
      myData$Percentage <- (myData$var1 / myData$var2)*100; #percentage calc based on var1 and var2;

    • Eduardo says:

      Actually the 4:5 is not necessary, you could just use saida["MY_NEW_COLUMN"] <- NEW_VALUE_HERE.

  7. mcmann says:

    how do i compute the log return of column D in a new column?that is the new column must be :

    column D formula is { log(n row in column D / (n-1) row in column D) }

  8. Maartje says:

    How would I add several columns to my dataframe?
    I have a dataframe with the same number of rows, that i want to add to my existing dataframe.
    Normally i would just use this:

    But this does not work when i want to add more than one column (data.frame_x)

    Thank you in advance.

  9. cristy says:

    Hello Eduardo, I was wondering if you could advise how to create a new column based on values in a single column.
    For example, I have a column of interest rates from successive years – I’d like to create a new column with single values equal to the differences between values in successive years. Interest rate at (t)- Interest rate at (t-1), only for observations with t>=2.
    Thank you in advance, I’d really appreciate your assistance

    • Eduardo says:

      I suggest your looking this thread here. The person there was looking for a solution of a problem like yours. Some nice answers were said there and I think you can base your solution on that. Cheers!

  10. MLS student says:

    Wow, thank you so much for this explanation! I’m taking a data science course and the professor is more preoccupied with filling our heads with lamda calculus and other theoretical junk that we have not been shown how to do even the most basic functions in R. I have had to learn everything from scratch by spending countless hours outside of class Googling things. I tried stackoverflow but the answers are always so cryptic and advanced and the attitude and atmosphere on stackoverflow is extremely unfriendly and discouraging. So, thank you so much!

    • Eduardo says:

      Hi, great that this post was helpful. I hear you, I’ve studied Computer Science (and I still do) so I know well how, sometimes, the theoretical and practical sides are so unbalanced. Thanks for your comment. You are welcome!

  11. pat666 says:

    Nice one. Thanks. Looked for ages for a solution to this.

  12. Apurva says:

    Hi, what if I want to add the value “1” from rows 1 to 10 and value “2” for rows 10 onwards under this new column?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: