Reputation: 35
I have a dataframe with the following structure.
FECHA FECHA_EXCEL
1 2015-05-25 12:00:00 42149
2 2015-05-26 12:00:00 42150
3 2015-05-27 12:00:00 42151
4 2015-05-28 12:00:00 42152
5 2015-05-29 12:00:00 42153
6 2015-06-01 12:00:00 42156
I want to add to this dataframe columns that are generated from my code.
I have like 45 products and I want to add 45 new columns to my dataframe and in its column name I want the name of each product.
For example for my product 1 I have a Dataframe with the following structure.
FECHA FECHA_EXCEL sd mean Rto_diario Rto_ventana senial_sigma Volatility_rol precio_M_Mean_menos3Sigma
1 2015-05-25 12:00:00 42149 NA NA NA NA NA NA NA
2 2015-05-26 12:00:00 42150 NA NA -0.007242345 NA NA NA NA
3 2015-05-27 12:00:00 42151 NA NA 0.016974636 NA NA NA NA
4 2015-05-28 12:00:00 42152 NA NA -0.004225315 NA NA NA NA
5 2015-05-29 12:00:00 42153 NA NA -0.014513125 NA NA NA NA
6 2015-06-01 12:00:00 42156 NA NA 0.001827485 NA NA NA
I want to add to my new dataframe my column named as "Volatilily_Rol" and its column name should be the name of this product, the product name is saved in a variable called, "product_name".
Here is my code for creating this new dataframe.
dfRolVoling (it is my new DF) dfsigma_ (old DF where I obtain the column I want to add)
cbind(dfRolVoling, product_name=dfsigma_$Volatility_rol)
But in my new DF isn't not appearing its name correctly, do you know any function where I can add a new COL to my dataframe with a concrete name?
It's my new DF using this function.
FECHA FECHA_EXCEL product_name
1 2015-05-25 12:00:00 42149 NA
2 2015-05-26 12:00:00 42150 NA
3 2015-05-27 12:00:00 42151 NA
4 2015-05-28 12:00:00 42152 NA
5 2015-05-29 12:00:00 42153 NA
6 2015-06-01 12:00:00 42156 NA
Product_name colname should be it's correct name.
Thanks.
Upvotes: 0
Views: 56
Reputation: 146
If you want to have this result you can simply add the column by using df$colname <- NA
This will setup your new column. In your case it should be dfRolVoling$product_name <- NA
. That should do the trick.
Upvotes: 1