Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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中的IF-返回功能_R - Fatal编程技术网

体育书籍R中的IF-返回功能

体育书籍R中的IF-返回功能,r,R,我有以下数据集(pre_df),我想要: 当选择时=主页-在新列中返回APL(在本例中为主页) 当选择=离开时-在新列中返回APL 当选择时=绘制-在新列中返回APL 我已经做了第一个示例(Home\u APL),但是当我为Away\u APL运行相同的IF函数时,它不起作用 #Home_APL for (row in 1:nrow(pre_df)) { if(pre_df$selection[row] == pre_df$home[row]) {

我有以下数据集(pre_df),我想要:

选择时=主页-在新列中返回APL(在本例中为主页)

选择=离开时-在新列中返回APL

选择时=绘制-在新列中返回APL

我已经做了第一个示例(Home\u APL),但是当我为Away\u APL运行相同的IF函数时,它不起作用

#Home_APL
for (row in 1:nrow(pre_df)) 

{ 
    if(pre_df$selection[row] == pre_df$home[row])
        {
        pre_df$Home_APL[row] = pre_df$APL[row]
        } 
}


#Away_APL
for (row in 1:nrow(pre_df)) 
{ 
    if(pre_df$selection[row] == pre_df$away[row])
        {
        pre_df$Away_APL[row] = pre_df$APL[row]
        } 
}

就像@r2evans所说的,让你的问题重现。并指定当您说“它不工作”时出现的错误

它失败的原因可能是因为您正在添加只填充了部分行的新列。因此R不知道如何填充缺少的行,您将得到一个

Error in `$<-.data.frame`(`*tmp*`, "Away_APL", value = c(NA, 10.4752232550593 : 
  replacement has 2 rows, data has 1153

请提供一份报告。这特别(在我看来)排除了数据图像的使用:(1)你要求我们将图像中的数据转录成有用的东西,这是不会发生的;(2) 实际上,它不显示数据的实际类别(例如,
因子
数值
字符
POSIXt
);(3) 您可以像粘贴图像一样轻松地执行dput(head(x,n=10))并粘贴它。认真地说,请阅读链接,然后编辑你的问题。这是.csv文件:评论中的数据很难做好。指向外部文件的链接(评论或问题中的链接)经常会出错,导致问题无法生成。你能提供问题本身的样本数据吗?
pre_df$Home_APL <- 0
pre_df$Away_APL <- 0
# > head(pre_df, 10)
#     X       date eventId      selection           home           away toverByPrice  turnover       APL Home_APL  Away_APL
# 1   1 01/01/2017 4414294        Arsenal        Arsenal Crystal Palace     61883.68  45861.26  1.349367 1.349367  0.000000
# 2   2 01/01/2017 4414294 Crystal Palace        Arsenal Crystal Palace    184655.35  17627.82 10.475223 0.000000 10.475223
# 3   3 01/01/2017 4414294           Draw        Arsenal Crystal Palace     41557.02   7540.92  5.510868 0.000000  0.000000
# 4   4 01/01/2017 4414399           Draw        Watford      Tottenham     13851.13   3517.82  3.937418 0.000000  0.000000
# 5   5 01/01/2017 4414399      Tottenham        Watford      Tottenham    184797.52 114681.12  1.611403 0.000000  1.611403
# 6   6 01/01/2017 4414399        Watford        Watford      Tottenham     73569.21  12606.46  5.835834 5.835834  0.000000
# 7   7 01/02/2017 4413713           Draw Manchester Utd           Hull      8293.93   1130.00  7.339761 0.000000  0.000000
# 8   8 01/02/2017 4413713           Hull Manchester Utd           Hull    130880.46   7150.82 18.302860 0.000000 18.302860
# 9   9 01/02/2017 4413713 Manchester Utd Manchester Utd           Hull     17798.90  14842.22  1.199207 1.199207  0.000000
# 10 10 01/02/2017 4413852           Draw          Stoke        Everton     32646.77   9561.08  3.414548 0.000000  0.000000