首页 文章

从另一个数据框中获取值

提问于
浏览
0

我通过它在数据框(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

    它看起来像你只是想为每个 Ticker 获取最后一个值?如果是这样,只需使用 tapplydplyr::group_by . 如果没有,请澄清您的预期输出 .

    tapply解决方案

    with(data2017, tapply(Close, Ticker, function(x) x[length(x)]))
    
    PETR4 USIM5 VALE5 
    15.59  8.47 29.07
    

    dplyr :: group_by解决方案

    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
    
  • 0

    当我输入你的代码时,我没有收到错误 . 看一看:

    > 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
    

    从我看到的,您将从Ref列中分配data2017数据框中的最后一个股票代码值 .

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

相关问题