【发布时间】:2021-09-30 16:45:06
【问题描述】:
我有一个看起来像这样的参考股票代码表
我想调查每个“演出日期”前后的价格变化。
例如,我想为一个现有行添加 2 个新行,显示日期为 -1 和 + 1 天,基本上使表格膨胀 3 次。将所有其他列留空,除非确保正确添加了符号。
That is:
for first row ZTS 2017-01-09 Buy
I want to add 2 more rows ZTS 2017-01-08
ZTS 2017-01-10
For the next row ZTS 2016-11-02 Buy
add 2 more rows ZTS 2016-11-01
ZTS 2016-11-03
etc...
我阅读了有关 add_rows() 的信息,但该函数不允许我这样做。这篇文章也没有解决它 Add rows to a data-frame based on values in one of the columns
有人可以这样做吗?通常我只处理列.....
>dput(head(sanitised))
structure(list(Company = c("Agilent Technologies ", "Agilent
Technologies ",
"Agilent Technologies ", "Agilent Technologies ", "Agilent
Technologies ",
"Alcoa "), symbol = c("A", "A", "A", "A", "A", "AA"), showdate =
c("2021-08-18",
"2020-04-16", "2017-11-17", "2017-03-23", "2016-05-13", "2016-10-
07"
), call = c("Buy", "Buy", "Buy", "Buy", "Buy", "Buy"),
show_segment = c("Guest Interview",
"Guest Interview", "Discussed Stock", "Featured Stock",
"Discussed Stock",
"Discussed Stock"), call_price = c("$163.02", "$78.75", "$68.79",
"$53.18", "$42.49", "$31.37"), current_returns = c("+0.0%",
"+0.0%",
"+0.0%", "+0.0%", "+0.0%", "+0.0%"), day_7_returns = c("", "",
"", "+0.3%", "+5.7%", "-15.7%"), day_14_returns = c("", "", "",
"-0.1%", "", ""), day_30_returns = c("", "", "", "", "+6.8%",
"-19.7%")), row.names = c(NA, 6L), class = "data.frame")
【问题讨论】:
-
使用
dput()提供可重现的数据。图片遗漏了重要信息,例如您拥有什么样的日期对象。 -
@dcarlson 对此感到抱歉。就是这样,我在桌子上做了一个很大的装饰来展示头部
标签: r dataframe datetime data-wrangling