从另一个数据框中获取值

时间:2017-09-26 22:21:15

标签: r dataframe

我通过它在数据框(data2017)中的最后一个外观获得了元素的引用值,如下所示:

 data2017 <- data.frame(Ticker = c("PETR4","PETR4","VALE5","VALE5","USIM5","USIM5"),
                        Close  = c(14, 15.59, 28.10, 29.07, 8.50, 8.47))

tail(data2017[data2017$Ticker=="PETR4","Close"],1)

对于值为“PETR4”的行,它给了我关闭列的最后价格并且工作得很好。  但是当我不想自动完成时,使用下面的数据框中的其他代码填充新列的新行:

Reference_Prices <- data.frame(Ticker    = c("PETR4","VALE5","USIM5"),
                               Reference = c("NA","NA","NA"))



N <- nrow(Reference_Prices)
Reference_Prices$Ref <- tail(data2017[data2017$Ticker==Reference_Prices$Ticker[1:N],"Close"],1) 

它给了我以下错误:

  

较长的物体长度不是较短物体长度的倍数

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

当我输入您的代码时,我不会收到错误。看看:

&#13;
&#13;
> data2017 <- data.frame(Ticker=c("PETR4","PETR4","VALE5","VALE5","USIM5","USIM5"),Close=c("14","15.59","28.10","29.07","8.50","8.47"))
> data2017
  Ticker Close
1  PETR4    14
2  PETR4 15.59
3  VALE5 28.10
4  VALE5 29.07
5  USIM5  8.50
6  USIM5  8.47
> tail(data2017[data2017$Ticker=="PETR4","Close"],1)
[1] 15.59
Levels: 14 15.59 28.10 29.07 8.47 8.50
> str(data2017)
'data.frame':	6 obs. of  2 variables:
 $ Ticker: Factor w/ 3 levels "PETR4","USIM5",..: 1 1 3 3 2 2
 $ Close : Factor w/ 6 levels "14","15.59","28.10",..: 1 2 3 4 6 5
> tail(data2017[data2017$Ticker=="PETR4","Close"])
[1] 14    15.59
Levels: 14 15.59 28.10 29.07 8.47 8.50
> Reference_Prices <- data.frame(Ticker=c("PETR4","VALE5","USIM5"),Reference=c("NA","NA","NA"))
> Reference_Prices
  Ticker Reference
1  PETR4        NA
2  VALE5        NA
3  USIM5        NA
> str(Reference_Prices)
'data.frame':	3 obs. of  2 variables:
 $ Ticker   : Factor w/ 3 levels "PETR4","USIM5",..: 1 3 2
 $ Reference: Factor w/ 1 level "NA": 1 1 1
> N <- nrow(Reference_Prices) 
> tail(data2017[data2017$Ticker==Reference_Prices$Ticker[1:N],"Close"],1)
[1] 8.47
Levels: 14 15.59 28.10 29.07 8.47 8.50
> Reference_Prices$Ref <- tail(data2017[data2017$Ticker==Reference_Prices$Ticker[1:N],"Close"],1)
> Reference_Prices
  Ticker Reference  Ref
1  PETR4        NA 8.47
2  VALE5        NA 8.47
3  USIM5        NA 8.47
> data2017$Ticker==Reference_Prices$Ticker[1:N]
[1]  TRUE FALSE FALSE FALSE FALSE  TRUE
> data2017[data2017$Ticker==Reference_Prices$Ticker[1:N],"Close"]
[1] 14   8.47
Levels: 14 15.59 28.10 29.07 8.47 8.50
&#13;
&#13;
&#13;

从我看到的情况来看,您正在为Ref列分配data2017数据框中的最后一个股票代码值。

尝试使用错误消息粘贴控制台的打印输出。此外,在调试时将代码分解为更容易消化的代码段。看看我在下面做了什么。

答案 1 :(得分:0)

在我看来,您只是想尝试获取每个Ticker的最后一个值?如果是,请使用tapplydplyr::group_by。如果没有,请澄清您的预期输出。

tapply solution

with(data2017, tapply(Close, Ticker, function(x) x[length(x)]))

PETR4 USIM5 VALE5 
15.59  8.47 29.07 

dplyr :: group_by solution

library(dplyr)
data2017 %>% 
  group_by(Ticker) %>% 
  summarize(last_close = Close[n()])

# A tibble: 3 x 2
  Ticker last_close
  <fctr>      <dbl>
1  PETR4      15.59
2  USIM5       8.47
3  VALE5      29.07