如何在不同data.Frame中的多个列上应用na.locf,所有列均以“结束”;价格“;
我有一个包含几个数据框的列表,每个数据框包含两个组件“日期”和“XXX价格”——例如波音价格、通用价格 它们不在一张纸上。每家公司都有自己的工作表 现在我想在所有公司的“价格”栏上应用na.locf。比如:如何在不同data.Frame中的多个列上应用na.locf,所有列均以“结束”;价格“;,r,R,我有一个包含几个数据框的列表,每个数据框包含两个组件“日期”和“XXX价格”——例如波音价格、通用价格 它们不在一张纸上。每家公司都有自己的工作表 现在我想在所有公司的“价格”栏上应用na.locf。比如: Q <- list(Boeing, GM; ...) Select from every company the column ending with "_price" and apply n.locf. Afterwards I would like to add another
Q <- list(Boeing, GM; ...)
Select from every company the column ending with "_price" and apply n.locf.
Afterwards I would like to add another column diff(log(XXX_price)).
Finally I would like to add all return columns in on sheet.
有人知道怎么做吗?使用结尾注释中的数据尝试以下操作:
library(zoo)
f <- function(data) {
data[[2]] <- na.locf0(data[[2]])
data
}
L2 <- lapply(L, f)
图书馆(动物园)
F
library(zoo)
f <- function(data) {
data[[2]] <- na.locf0(data[[2]])
data
}
L2 <- lapply(L, f)
library(zoo)
z <- na.locf(read.zoo(do.call("merge", L)), na.rm = FALSE)
library(xts)
X <- as.xts(z)
Lines1 <- "Date Boeing_price
12.11.2010 53.478
15.11.2010 53.918
16.11.2010 53.215
17.11.2010 52.978
18.11.2010 54.766
19.11.2010 53.901
22.11.2010 54.274
23.11.2010 53.91
24.11.2010 55.444
26.11.2010 54.927
29.11.2010 54.554
30.11.2010 54.054"
DF1 <- read.table(text = Lines1, header = TRUE, na.strings = "#NA")
DF1$Date <- as.Date(DF1$Date, "%d.%m.%Y")
Lines2 <- "Date GM_Price
12.11.2010 #NA
15.11.2010 #NA
16.11.2010 #NA
17.11.2010 28.5455
18.11.2010 29.5749
19.11.2010 29.6355
22.11.2010 #NA
23.11.2010 28.7618
24.11.2010 28.9607
26.11.2010 29.2376
29.11.2010 #NA
30.11.2010 29.5836"
DF2 <- read.table(text = Lines2, header = TRUE, na.strings = "#NA",
comment.char = "")
DF2$Date <- as.Date(DF2$Date, "%d.%m.%Y")
L <- list(DF1, DF2)