Add columns in a data frame – R

If you have a data frame and you wish to add a column then here is how you should do it:


Lets say your data frame is

> data
       date_ seq value_ value_.1
1 2009-03-31   1   0.39     0.39
2 2009-06-30   2   0.41     0.41
3 2009-09-30   3   0.45     0.45
4 2009-12-31   4   0.36     0.36
5 2010-03-31   1   0.30     0.30
6 2010-06-30   2   0.34     0.34
7 2010-09-30   3   0.31     0.31

Now you want to add a column “name”, do:

> data$name < - "AA"
> data
       date_ seq value_ value_.1 name
1 2009-03-31   1   0.39     0.39   AA
2 2009-06-30   2   0.41     0.41   AA
3 2009-09-30   3   0.45     0.45   AA
4 2009-12-31   4   0.36     0.36   AA
5 2010-03-31   1   0.30     0.30   AA
6 2010-06-30   2   0.34     0.34   AA
7 2010-09-30   3   0.31     0.31   AA
Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*