# Adding New Column To A Data Frame in R

March 12, 2010 19 Comments

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.

Advertisements

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

thanks! good post

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

Thanks, this was useful!

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

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.

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;

Indeed, it is not necessary. Thanks for pointing out 🙂

Actually the 4:5 is not necessary, you could just use

`saida["MY_NEW_COLUMN"] <- NEW_VALUE_HERE`

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

log(0/3)

log(3/3)

log(3/1)

log(1/4)

log(4/0)

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

sorry correct formula is { log(n row in column D / (n+1) row in column D) }

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:

dataframe.x$newcolumn<-variable_x

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

Thank you in advance.

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

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

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!

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!

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!

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

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?