首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >R减去向量-数字0答案

R减去向量-数字0答案
EN

Stack Overflow用户
提问于 2015-11-06 22:44:58
回答 1查看 1.3K关注 0票数 0

我试着从另一个向量中减去两个向量,每个向量长度为7。然而,我的结果不会产生另一个向量,我只得到数字0和数据0。有什么想法吗?

代码语言:javascript
运行
复制
#You will buy these stocks on rows/days 7, 8, 9, 15, 26, 33 and 53. (1 day after days apple dropped more than 2% )
#Assign the prices of the stocks on these days to a vector
SLB_buy = c(SLB_close[7],SLB_close[8],SLB_close[9],SLB_close[15],SLB_close[26],SLB_close[34],SLB_close[53])
SLB_buy
           SLB.Close
2015-08-21     77.50
2015-08-24     73.87
2015-08-25     72.52
2015-09-02     75.41
2015-09-18     72.54
2015-09-30     68.97
2015-10-27     76.95

#You will sell these stocks on rows/days 10, 11, 12, 18, 29, 36 and 56. (3 day after purchase)
#Assign the prices of the stocks on these days to a vector
SLB_sell =c(SLB_close[10],SLB_close[11],SLB_close[12],SLB_close[18],SLB_close[29],SLB_close[36],SLB_close[56])
SLB_sell
       SLB.Close
2015-08-26     70.09
2015-08-27     73.85
2015-08-28     76.06
2015-09-08     75.54
2015-09-23     71.94
2015-10-02     70.32
2015-10-30     78.16

#Your profit is equal to the sales price - buy price. Hence substract the previous vectors from each other.
SLB_sell - SLB_buy
Data:
numeric(0)

Index:
numeric(0)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-06 23:29:44

我从剪贴板上复制了你的矢量。我为实现你想要的而做的事:

代码语言:javascript
运行
复制
# remove SLB.Close, which is the first element of the vector. 
#It might not be the case for your actual vector though.
SLB_buy<-SLB_buy[-1]
# remove the dates and transform in numeric

SLB_buy<-substring(SLB_buy, 11)
SLB_buy<-as.numeric(SLB_buy) 

# depending on your actual vector the position to start substring 
#(in this case 11) might vary.

对第二个向量做同样的操作并减去

代码语言:javascript
运行
复制
SLB_sell-SLB_buy
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33576896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档