R:如何通过操作日期时间列在数据框中添加行
R: how to add rows in dataframe by manipulating a datetime column
我有一个 table 的参考股票代码,看起来像这样
我想调查每个“展示日期”前后的价格变化。
例如,我想为一个现有行添加 2 个新行,显示日期为 -1 和 + 1 天,本质上是将 table 膨胀 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() 的内容,但该函数不允许我这样做。这个 post 也没有解决
有人能做到吗?通常我只处理列.....
>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("3.02", ".75", ".79",
".18", ".49", ".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")
这是一种使用 split
和 map_dfr
的方法。我假设对于添加的行,大多数列中的值将是 NA
。您当然可以在 add_row
.
中添加其他值
library(tidyverse)
sanitised %>%
mutate(showdate = as.Date(showdate, format = '%Y-%m-%d')) %>%
split(list(sanitised$symbol, sanitised$showdate)) %>%
map_dfr(function(x) {
x %>%
# any other columns you want to keep could go in here as well
add_row(symbol = x$symbol, showdate = x$showdate - 1) %>%
add_row(symbol = x$symbol, showdate = x$showdate + 1)
})
这是一个带有循环的基本 R 方法,假设“symbol”是唯一复制的列:
x$showdate <- as.Date(x$showdate)
for(i in rownames(x)){
x[paste0(i, ".", 1), "showdate"] <- x[i, "showdate"] - 1
x[paste0(i, ".", 2), "showdate"] <- x[i, "showdate"] + 1
x[paste0(i, ".", 1), "symbol"] <- x[i, "symbol"]
x[paste0(i, ".", 2), "symbol"] <- x[i, "symbol"]
}
x <- x[order(rownames(x)), ]
我有一个 table 的参考股票代码,看起来像这样
我想调查每个“展示日期”前后的价格变化。
例如,我想为一个现有行添加 2 个新行,显示日期为 -1 和 + 1 天,本质上是将 table 膨胀 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() 的内容,但该函数不允许我这样做。这个 post 也没有解决
有人能做到吗?通常我只处理列.....
>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("3.02", ".75", ".79",
".18", ".49", ".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")
这是一种使用 split
和 map_dfr
的方法。我假设对于添加的行,大多数列中的值将是 NA
。您当然可以在 add_row
.
library(tidyverse)
sanitised %>%
mutate(showdate = as.Date(showdate, format = '%Y-%m-%d')) %>%
split(list(sanitised$symbol, sanitised$showdate)) %>%
map_dfr(function(x) {
x %>%
# any other columns you want to keep could go in here as well
add_row(symbol = x$symbol, showdate = x$showdate - 1) %>%
add_row(symbol = x$symbol, showdate = x$showdate + 1)
})
这是一个带有循环的基本 R 方法,假设“symbol”是唯一复制的列:
x$showdate <- as.Date(x$showdate)
for(i in rownames(x)){
x[paste0(i, ".", 1), "showdate"] <- x[i, "showdate"] - 1
x[paste0(i, ".", 2), "showdate"] <- x[i, "showdate"] + 1
x[paste0(i, ".", 1), "symbol"] <- x[i, "symbol"]
x[paste0(i, ".", 2), "symbol"] <- x[i, "symbol"]
}
x <- x[order(rownames(x)), ]