Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在数据帧中使用迭代_R_Dataframe_Iteration - Fatal编程技术网

R 在数据帧中使用迭代

R 在数据帧中使用迭代,r,dataframe,iteration,R,Dataframe,Iteration,我在处理股票(何时买入/卖出,持有多少股票) 我是一个乞丐,试图从Excel中的小样本量转移到R中的大样本量 我将尝试用一个可复制的例子来解释我的问题。如果无法理解,请让我知道,我将尝试澄清 我有两种股票: AAPL.price <- c(140,145,150,148,152,156,153) MSFT.price <- c(70,75,73,74,73,76,74) price <- data.frame (AAPL.price,MSFT.price) 这将影响[i]期间

我在处理股票(何时买入/卖出,持有多少股票)

我是一个乞丐,试图从Excel中的小样本量转移到R中的大样本量

我将尝试用一个可复制的例子来解释我的问题。如果无法理解,请让我知道,我将尝试澄清

我有两种股票:

AAPL.price <- c(140,145,150,148,152,156,153)
MSFT.price <- c(70,75,73,74,73,76,74)
price <- data.frame (AAPL.price,MSFT.price)
这将影响[i]期间的现金,从而影响[i+1]中的股票数量,依此类推

有人能帮我吗

对不起,如果这太让人困惑了,我尽量说清楚


提前感谢您

如果我没弄错,您在下一步实现时会遇到问题:
decision2$cash[i-1]/decision2$numberbuys[i]/price

输入
谢谢你的帮助,但实际上这不是我的问题。我的问题是如何使决策2的第一列和第二列(AAPL和MSFT)依赖于以前的现金价值。我想将您创建的“res”列除以每只股票的价格。然后我想在我卖出股票时,重新评估相同数量股票的价值,这将对现金产生影响。我说清楚了吗?我为两个股票符号都添加了
res/price
列。我希望你能自己重新评估这些价值观。谢谢你的帮助。然而,这不是我想要的。因为我的挣扎是,例如,在时间3,我买了10145支微软金融公司的股票,在时间5,我卖掉了这些股票。这些股票的出售会影响我在时间5时的现金水平,这反过来会影响我在时间7时购买的MSFT股票。你所做的只是用现金水平[i-1]来决定买入的股票数量[i],但我还需要卖出的股票的价格和数量[i]来影响现金水平[i]
AAPL <- c(1,-1,0,1,0,0,-1)
MSFT <- c(0,0,1,0,-1,0,1)
decision <- data.frame (AAPL,MSFT)
numberstocks <- function(x) {

newValue <- 0

for (i in 1:length(x)) {

    if (x[i] == -1) {
        newValue <- -100
    } else if (x[i] == 1) {
        newValue <- 100
    } else if (x[i] == 0 && newValue != 100) {
        newValue <- 0
    }
    x[i] <- newValue
}

return(x)

}
positivevalues <- function (x)
sapply (seq_along(x),function (i) {
    if (x[i] < -0.5) {
        100
    }else{
        x[i]
    }
})

decision3 <- as.data.frame (lapply(decision2[,1:2], positivevalues))
decision2$cashmov <- with (decision2, c(rowSums(decision*price*decision3)))

decision2$cash <- with (decision2, c(1000000, matrix(0,6,1)))
decision2$cash <- 0
    for (i in 1) {
    decision2$cash [i] = 1000000
    }
    for (i in 2:7){
    decision2$cash [i] = decision2$cash[i-1]-decision2$cashmov[i]
}        
decision2$numberbuys <- with (decision2, c(rowSums(decision>0)))
decision2$cash[i-1]/decision2$numberbuys[i]/price
con <- textConnection("AAPL MSFT    cash cashmov numberbuys
100    0 1000000   14000          1
-100    0 1014500  -14500          0
   0  100 1007200    7300          1
 100  100  992400   14800          1
 100 -100  999700   -7300          0
 100    0  999700       0          0
-100  100 1007600   -7900          1")

decision2 <- read.table(con, header = T)

df <- data.frame(cash = decision2$cash, numberbuys = decision2$numberbuys)

#cash.offset column is decision2$cash[i-1]
df$cash.offset <- c(NA, df$cash[1:nrow(df) - 1])
df$res <- df$cash.offset / df$numberbuys

df$AAPL <- df$res / decision2$AAPL
df$MSFT <- df$res / decision2$MSFT

df$AAPL[is.infinite(df$AAPL)] <- 0
df$MSFT[is.infinite(df$MSFT)] <- 0

print(df)
     cash numberbuys cash.offset     res  AAPL  MSFT
1 1000000          1          NA      NA    NA    NA
2 1014500          0     1000000     Inf     0     0
3 1007200          1     1014500 1014500     0 10145
4  992400          1     1007200 1007200 10072 10072
5  999700          0      992400     Inf     0     0
6  999700          0      999700     Inf     0     0
7 1007600          1      999700  999700 -9997  9997