以下是我的数据集
structure(list(atp = c(1, 0, 1, 0, 0, 1), len = c(2, NA, 3, NA,
NA, 1), inv = c(593, 823, 668, 640, 593, 745), GU = c(36, 94,
57, 105, 48, 67), RUTL = c(100, NA, 173, NA, NA, 7)), .Names = c("atp",
"len", "inv", "GU", "RUTL"), row.names = c(NA, -6L), class = "data.frame")
我需要执行以下函数的逐行操作
I was able to achieve the desired result using for loop
以下是代码返回
for (i in 1:nrow(transit))
{
if (transit[["atp"]][i]==1)
{
transit[["csi_begin"]][i]<-(transit[["inv"]])[i]
transit[["csi_end"]][i]<-transit[["csi_begin"]][i]-transit[["GU"]][i]
if (i-2<=0) { transit[["csi_order"]][i]<-0
transit[["IRQ"]][i]<-transit[["csi_order"]][i]-transit[["RUTL"]][i]
} else
{
if (transit[["csi_begin"]][i-2]>= -878)
{
transit[["csi_order"]][i]<-transit[["csi_begin"]][i-2]
} else
{ transit[["csi_order"]][i]<-0}
transit[["IRQ"]][i]<-transit[["csi_order"]][i]-transit[["RUTL"]][i]
}
} else
{
transit[["csi_begin"]][i]<-(transit[["inv"]])[i]+transit[["IRQ"]][i-1]
transit[["csi_end"]][i]<-transit[["csi_begin"]][i]-transit[["GU"]][i]
if (i-2<=0) { transit[["csi_order"]][i]<-0
transit[["IRQ"]][i]<-0
} else
{
if (transit[["csi_begin"]][i-2]>= -878)
{
transit[["csi_order"]][i]<-transit[["csi_begin"]][i-2]
} else
{ transit[["csi_order"]][i]<-0}
transit[["IRQ"]][i]<-0
}}
}
任何人都可以帮助我摆脱这个for循环以及如何为数据表编写相同的代码,以便它非常有效。