使用变量在R中添加数据框列

时间:2021-11-03 06:02:34

I am trying to achieve the following

我正在努力实现以下目标

stocks <- c('AXP', 'VZ', 'V')
library('quantmod')
getSymbols(stocks)

Above command creates 3 data variables named AXP, VZ, and V

上面的命令创建了3个名为AXP,VZ和V的数据变量

prices <- data.frame(stringAsFactors=FALSE)

Here I am trying to create a column with name as ticket (e.g. AXP) with data in The following should add 3 columns to the frame, names AXP, VZ, and V with data in AXP$AXP.Adjusted, VZ$VZ.Adjusted, V$V.Adjusted

在这里,我尝试创建一个名称为ticket(例如AXP)的列,其中包含数据以下内容应该向框架添加3列,将AXP,VZ和V的名称命名为AXP $ AXP.Adjusted,VZ $ VZ.Adjusted中的数据,V $ V.Adjusted

for (ticker in stocks)
{
  prices$ticker <- ticker$ticker.Adjusted
}

How do I achieve this? R gives an error like this when I try this

我该如何实现这一目标?当我尝试这个时,R会给出这样的错误

Error in ticker$ticker.Adjusted : 
  $ operator is invalid for atomic vectors

Any ideas?

有任何想法吗?

Thanks in advance

提前致谢

3 个解决方案

#1


1  

Here is a simpler way to do this

这是一种更简单的方法

do.call('cbind', lapply(mget(stocks), function(d) d[,6]))

Explanation:

说明:

  1. mget(stocks) gets the three data frames as a list
  2. mget(stocks)将三个数据框作为列表
  3. lapply extracts the 6th column which contains the variable of interest.
  4. lapply提取包含感兴趣变量的第6列。
  5. do.call passes the list from (2) to cbind, which binds them together as columns.
  6. do.call将列表从(2)传递给cbind,它将它们作为列绑定在一起。

NOTE: This solution does not take care of the different number of columns in the data frames.

注意:此解决方案不会处理数据框中的不同列数。

#2


0  

I did not understand your question before, now I think I understood what you want:

我以前不理解你的问题,现在我想我理解你想要的东西:

What you wrote does not work because the object ticker is character string. If you want to get the object named after that string, you have to evaluate the parsed text.

你写的东西不起作用,因为对象自动收报机是字符串。如果要获取以该字符串命名的对象,则必须评估已解析的文本。

Try this:

尝试这个:

for (ticker in stocks){
      prices <- cbind(prices, eval(parse(text=ticker))[,paste0(ticker, ".", "Adjusted")])

}

This will give you:

这会给你:

An ‘xts’ object on 2007-01-03/2014-01-28 containing:
  Data: num [1:1780, 1:4] 53.4 53 52.3 52.8 52.5 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "AXP.Adjusted" "AXP.Adjusted.1" "VZ.Adjusted" "V.Adjusted"
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2014-01-29 01:06:51"

#3


0  

One problem you're going to have is that the three downloads have different number of rows, so binding them all into a single data frame will fail.

您将遇到的一个问题是三次下载具有不同的行数,因此将它们全部绑定到单个数据框中将会失败。

The code below uses the last 1000 rows of each file (most recent), and does not use loops.

下面的代码使用每个文件的最后1000行(最近的),并且不使用循环。

stocks <- c('AXP', 'VZ', 'V')
library('quantmod')
getSymbols(stocks)

prices=do.call(data.frame,
               lapply(stocks,
                      function(s)tail(get(s)[,paste0(s,".Adjusted")],1000)))
colnames(prices)=stocks
head(prices)
#              AXP    VZ     V
# 2010-02-08 34.70 21.72 80.58
# 2010-02-09 35.40 22.01 80.79
# 2010-02-10 35.60 22.10 81.27
# 2010-02-11 36.11 22.23 82.73
# 2010-02-12 36.23 22.15 82.38
# 2010-02-16 37.37 22.34 83.45

Working from the inside out, s is the ticker (so, e.g., "AXP"); get(s) returns the object with that name, so AXP; get(s)[,paste0(s,".Adjusted")] is equivalent to AXP[,"AXP.Adjusted"]; tail(...,1000) returns the last 1000 rows of .... So when s="AXP", the function returns the last 1000 rows of AXP$AXP.Adjusted.

从内到外工作,s是股票代码(例如,“AXP”); get(s)返回具有该名称的对象,因此AXP; get(s)[,paste0(s,“。Adjusted”)]相当于AXP [,“AXP.Adjusted”]; tail(...,1000)返回最后1000行......所以当s =“AXP”时,该函数返回AXP $ AXP.Adjusted的最后1000行。

lapply(...) applies that function to each element in stocks.

lapply(...)将该函数应用于股票中的每个元素。

do.call(data.frame,...) invokes the data.frame function with the list of columns returned by lapply(...).

do.call(data.frame,...)使用lapply(...)返回的列列表调用data.frame函数。

#1


1  

Here is a simpler way to do this

这是一种更简单的方法

do.call('cbind', lapply(mget(stocks), function(d) d[,6]))

Explanation:

说明:

  1. mget(stocks) gets the three data frames as a list
  2. mget(stocks)将三个数据框作为列表
  3. lapply extracts the 6th column which contains the variable of interest.
  4. lapply提取包含感兴趣变量的第6列。
  5. do.call passes the list from (2) to cbind, which binds them together as columns.
  6. do.call将列表从(2)传递给cbind,它将它们作为列绑定在一起。

NOTE: This solution does not take care of the different number of columns in the data frames.

注意:此解决方案不会处理数据框中的不同列数。

#2


0  

I did not understand your question before, now I think I understood what you want:

我以前不理解你的问题,现在我想我理解你想要的东西:

What you wrote does not work because the object ticker is character string. If you want to get the object named after that string, you have to evaluate the parsed text.

你写的东西不起作用,因为对象自动收报机是字符串。如果要获取以该字符串命名的对象,则必须评估已解析的文本。

Try this:

尝试这个:

for (ticker in stocks){
      prices <- cbind(prices, eval(parse(text=ticker))[,paste0(ticker, ".", "Adjusted")])

}

This will give you:

这会给你:

An ‘xts’ object on 2007-01-03/2014-01-28 containing:
  Data: num [1:1780, 1:4] 53.4 53 52.3 52.8 52.5 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "AXP.Adjusted" "AXP.Adjusted.1" "VZ.Adjusted" "V.Adjusted"
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2014-01-29 01:06:51"

#3


0  

One problem you're going to have is that the three downloads have different number of rows, so binding them all into a single data frame will fail.

您将遇到的一个问题是三次下载具有不同的行数,因此将它们全部绑定到单个数据框中将会失败。

The code below uses the last 1000 rows of each file (most recent), and does not use loops.

下面的代码使用每个文件的最后1000行(最近的),并且不使用循环。

stocks <- c('AXP', 'VZ', 'V')
library('quantmod')
getSymbols(stocks)

prices=do.call(data.frame,
               lapply(stocks,
                      function(s)tail(get(s)[,paste0(s,".Adjusted")],1000)))
colnames(prices)=stocks
head(prices)
#              AXP    VZ     V
# 2010-02-08 34.70 21.72 80.58
# 2010-02-09 35.40 22.01 80.79
# 2010-02-10 35.60 22.10 81.27
# 2010-02-11 36.11 22.23 82.73
# 2010-02-12 36.23 22.15 82.38
# 2010-02-16 37.37 22.34 83.45

Working from the inside out, s is the ticker (so, e.g., "AXP"); get(s) returns the object with that name, so AXP; get(s)[,paste0(s,".Adjusted")] is equivalent to AXP[,"AXP.Adjusted"]; tail(...,1000) returns the last 1000 rows of .... So when s="AXP", the function returns the last 1000 rows of AXP$AXP.Adjusted.

从内到外工作,s是股票代码(例如,“AXP”); get(s)返回具有该名称的对象,因此AXP; get(s)[,paste0(s,“。Adjusted”)]相当于AXP [,“AXP.Adjusted”]; tail(...,1000)返回最后1000行......所以当s =“AXP”时,该函数返回AXP $ AXP.Adjusted的最后1000行。

lapply(...) applies that function to each element in stocks.

lapply(...)将该函数应用于股票中的每个元素。

do.call(data.frame,...) invokes the data.frame function with the list of columns returned by lapply(...).

do.call(data.frame,...)使用lapply(...)返回的列列表调用data.frame函数。

相关文章