使用特定格式将新列添加到数据框

时间:2016-05-09 16:08:59

标签: r dataframe

我在下面有一个示例数据框:

df <- data.frame(ID=c("131","156","178","196"), Date=c("03/31/2016","03/31/2016","03/31/2016","03/31/2016"))
df
   ID       Date
1 131 03/31/2016
2 156 03/31/2016
3 178 03/31/2016
4 196 03/31/2016

我想在此数据框中添加一个新列,其格式如下:=BDH("ID CUSIP", "PX_Last","Date")。新数据框应如下所示:

   ID       Date  NewCol
1 131 03/31/2016  =BDH("131 CUSIP", "PX_Last", "03/31/2016")
2 156 03/31/2016  =BDH("156 CUSIP", "PX_Last", "03/31/2016")
3 178 03/31/2016  =BDH("178 CUSIP", "PX_Last", "03/31/2016")
4 196 03/31/2016  =BDH("196 CUSIP", "PX_Last", "03/31/2016")

如您所见,IDDate来自现有数据框,但CUSIPPX_Last不是。请帮助,谢谢!

1 个答案:

答案 0 :(得分:2)

这样可以吗?

df$NewCol <- paste0("=BDH(\"", df$ID, " CUSIP\", \"PX_Last\", \"", df$Date, "\")")