在 R Studio 中更改行名称
Change row names in R Studio
我尝试将行名称从日期更改为字符;
library(quantmod)
mdate <- "2019-05-01"
edate <- "2019-05-08"
tickers <- c("MMM","C", "AAPL", "IBM", "AMZN")
rnames <- c("Open_1", "Open_2", "Open_3", "Open_4", "Open_D")
portfolioPrices <- NULL
for(ticker in tickers)
portfolioPrices <- cbind(portfolioPrices, getSymbols(ticker, from = mdate, to = edate, auto.assign = F)[,1])
colnames(portfolioPrices) = tickers
rownames(portfolioPrices) = rnames
这些代码给我:
如您所见,行名称未更改。我想将它们从日期更改为 rnames <- c("Open_1", "Open_2", "Open_3", "Open_4", "Open_D")
我该怎么做才能实现这一目标?
您可以使用 "timetk" 包中的 "tk_tbl" 函数将 xts 对象转换为数据框。
library(quantmod)
library(timetk)
mdate <- "2019-05-01"
edate <- "2019-05-08"
tickers <- c("MMM","C", "AAPL", "IBM", "AMZN")
rnames <- c("Open_1", "Open_2", "Open_3", "Open_4", "Open_D")
portfolioPrices <- NULL
for(ticker in tickers)
portfolioPrices <- cbind(portfolioPrices, getSymbols(ticker, from = mdate, to = edate, auto.assign = F)[,1])
#tk_tbl convert "xts" object to data frame, but the original dates will be transformed into a new column named "index"
timetk::tk_tbl(portfolioPrices)
# A tibble: 5 x 6
index MMM.Open C.Open AAPL.Open IBM.Open AMZN.Open
<date> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2019-05-01 189. 70.7 210. 141. 1933.
2 2019-05-02 184. 69.7 210. 140. 1913.
3 2019-05-03 186. 70.4 211. 140. 1949
4 2019-05-06 182. 69.1 204. 138. 1918.
5 2019-05-07 182. 69.3 206. 139. 1940.
#remove the "index" column and reassign colnames and rownames
portfolioPrices <- timetk::tk_tbl(portfolioPrices)[, -1]
colnames(portfolioPrices) = tickers
rownames(portfolioPrices) = rnames
我尝试将行名称从日期更改为字符;
library(quantmod)
mdate <- "2019-05-01"
edate <- "2019-05-08"
tickers <- c("MMM","C", "AAPL", "IBM", "AMZN")
rnames <- c("Open_1", "Open_2", "Open_3", "Open_4", "Open_D")
portfolioPrices <- NULL
for(ticker in tickers)
portfolioPrices <- cbind(portfolioPrices, getSymbols(ticker, from = mdate, to = edate, auto.assign = F)[,1])
colnames(portfolioPrices) = tickers
rownames(portfolioPrices) = rnames
这些代码给我:
如您所见,行名称未更改。我想将它们从日期更改为 rnames <- c("Open_1", "Open_2", "Open_3", "Open_4", "Open_D")
我该怎么做才能实现这一目标?
您可以使用 "timetk" 包中的 "tk_tbl" 函数将 xts 对象转换为数据框。
library(quantmod)
library(timetk)
mdate <- "2019-05-01"
edate <- "2019-05-08"
tickers <- c("MMM","C", "AAPL", "IBM", "AMZN")
rnames <- c("Open_1", "Open_2", "Open_3", "Open_4", "Open_D")
portfolioPrices <- NULL
for(ticker in tickers)
portfolioPrices <- cbind(portfolioPrices, getSymbols(ticker, from = mdate, to = edate, auto.assign = F)[,1])
#tk_tbl convert "xts" object to data frame, but the original dates will be transformed into a new column named "index"
timetk::tk_tbl(portfolioPrices)
# A tibble: 5 x 6
index MMM.Open C.Open AAPL.Open IBM.Open AMZN.Open
<date> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2019-05-01 189. 70.7 210. 141. 1933.
2 2019-05-02 184. 69.7 210. 140. 1913.
3 2019-05-03 186. 70.4 211. 140. 1949
4 2019-05-06 182. 69.1 204. 138. 1918.
5 2019-05-07 182. 69.3 206. 139. 1940.
#remove the "index" column and reassign colnames and rownames
portfolioPrices <- timetk::tk_tbl(portfolioPrices)[, -1]
colnames(portfolioPrices) = tickers
rownames(portfolioPrices) = rnames