添加列时动物园系列中的 If 语句
If statement in zoo series when adding a column
我有以下动物园系列:
head(prices.zoo)
JetFuel HeatingOil Spread
Sep 1996 0.682 0.6794 0.0026
Oct 1996 0.703 0.7307 -0.0277
Nov 1996 0.696 0.7261 -0.0301
Dec 1996 0.693 0.7171 -0.0241
Jan 1997 0.680 0.7142 -0.0342
Feb 1997 0.619 0.6081 0.0109
我的目标是添加第 4 列 Action,当 Spread>0 时为 1,当 Spread<0 时为 -1。我尝试了以下代码:
f <-function(x){
if(x>0) y=1
else y= -1
return(y)}
prices.zoo$Action <- sapply(prices.zoo$Spread,f)
一条警告消息弹出:“警告消息:
在 if (x > 0) y = 1000 else y = -100 中:
条件的长度 > 1,并且只会使用第一个元素”
结果显然不正确:
JetFuel HeatingOil Spread Action
Sep 1996 0.682 0.6794 0.0026 1
Oct 1996 0.703 0.7307 -0.0277 1
Nov 1996 0.696 0.7261 -0.0301 1
Dec 1996 0.693 0.7171 -0.0241 1
Jan 1997 0.680 0.7142 -0.0342 1
Feb 1997 0.619 0.6081 0.0109 1
但在我将动物园系列转换为数据框后,一切正常:
prices.zoo.df <- data.frame(prices.zoo)
prices.zoo.df$Action <- sapply(prices.zoo.df$Spread,f)
head(prices.zoo.df)
JetFuel HeatingOil Spread Action
Sep 1996 0.682 0.6794 0.0026 1
Oct 1996 0.703 0.7307 -0.0277 -1
Nov 1996 0.696 0.7261 -0.0301 -1
Dec 1996 0.693 0.7171 -0.0241 -1
Jan 1997 0.680 0.7142 -0.0342 -1
Feb 1997 0.619 0.6081 0.0109 1
谁能给我解释一下为什么会这样?有没有办法在不移动到数据框的情况下在动物园系列上实现同样的事情?
可重现的例子
z <- zoo(cbind(c1 = c(1,2,3,-1,-2)),Sys.Date()+0:4)
f <-function(x){
if(x>0) y=1
else y= -1
return(y)}
z$c2 <- sapply(z$c1,f)
^这个方法与 sapply 产生以下输出(不是我要找的):
c1 c2
2017-05-09 1 1
2017-05-10 2 1
2017-05-11 3 1
2017-05-12 -1 1
2017-05-13 -2 1
将上述动物园系列转换为数据框后,使用 sapply 的方法同样有效:
z <- data.frame(z)
z$c2 <- sapply(z$c1,f)
z
c1 c2
2017-05-09 1 1
2017-05-10 2 1
2017-05-11 3 1
2017-05-12 -1 -1
2017-05-13 -2 -1
以下任何一个都可以:
# 1
prices.zoo$Action <- sign(prices.zoo$Spread)
# 2
prices.zoo$Action <- (prices.zoo$Spread > 0) - (prices.zoo$Spread < 0)
# 3
prices.zoo$Action <- ifelse(prices.zoo$Spread > 0, 1, -1)
# 4
prices.zoo$Action <- sapply(coredata(prices.zoo$Spread), f)
通常我们不会使用 sapply
除非没有它就无法对问题进行矢量化。
我有以下动物园系列:
head(prices.zoo)
JetFuel HeatingOil Spread
Sep 1996 0.682 0.6794 0.0026
Oct 1996 0.703 0.7307 -0.0277
Nov 1996 0.696 0.7261 -0.0301
Dec 1996 0.693 0.7171 -0.0241
Jan 1997 0.680 0.7142 -0.0342
Feb 1997 0.619 0.6081 0.0109
我的目标是添加第 4 列 Action,当 Spread>0 时为 1,当 Spread<0 时为 -1。我尝试了以下代码:
f <-function(x){
if(x>0) y=1
else y= -1
return(y)}
prices.zoo$Action <- sapply(prices.zoo$Spread,f)
一条警告消息弹出:“警告消息: 在 if (x > 0) y = 1000 else y = -100 中: 条件的长度 > 1,并且只会使用第一个元素” 结果显然不正确:
JetFuel HeatingOil Spread Action
Sep 1996 0.682 0.6794 0.0026 1
Oct 1996 0.703 0.7307 -0.0277 1
Nov 1996 0.696 0.7261 -0.0301 1
Dec 1996 0.693 0.7171 -0.0241 1
Jan 1997 0.680 0.7142 -0.0342 1
Feb 1997 0.619 0.6081 0.0109 1
但在我将动物园系列转换为数据框后,一切正常:
prices.zoo.df <- data.frame(prices.zoo)
prices.zoo.df$Action <- sapply(prices.zoo.df$Spread,f)
head(prices.zoo.df)
JetFuel HeatingOil Spread Action
Sep 1996 0.682 0.6794 0.0026 1
Oct 1996 0.703 0.7307 -0.0277 -1
Nov 1996 0.696 0.7261 -0.0301 -1
Dec 1996 0.693 0.7171 -0.0241 -1
Jan 1997 0.680 0.7142 -0.0342 -1
Feb 1997 0.619 0.6081 0.0109 1
谁能给我解释一下为什么会这样?有没有办法在不移动到数据框的情况下在动物园系列上实现同样的事情?
可重现的例子
z <- zoo(cbind(c1 = c(1,2,3,-1,-2)),Sys.Date()+0:4)
f <-function(x){
if(x>0) y=1
else y= -1
return(y)}
z$c2 <- sapply(z$c1,f)
^这个方法与 sapply 产生以下输出(不是我要找的):
c1 c2
2017-05-09 1 1
2017-05-10 2 1
2017-05-11 3 1
2017-05-12 -1 1
2017-05-13 -2 1
将上述动物园系列转换为数据框后,使用 sapply 的方法同样有效:
z <- data.frame(z)
z$c2 <- sapply(z$c1,f)
z
c1 c2
2017-05-09 1 1
2017-05-10 2 1
2017-05-11 3 1
2017-05-12 -1 -1
2017-05-13 -2 -1
以下任何一个都可以:
# 1
prices.zoo$Action <- sign(prices.zoo$Spread)
# 2
prices.zoo$Action <- (prices.zoo$Spread > 0) - (prices.zoo$Spread < 0)
# 3
prices.zoo$Action <- ifelse(prices.zoo$Spread > 0, 1, -1)
# 4
prices.zoo$Action <- sapply(coredata(prices.zoo$Spread), f)
通常我们不会使用 sapply
除非没有它就无法对问题进行矢量化。