0

I have the following data set (pre_df) and I want:

pre_df

When selection = home - return APL in the new column (in this case Home_APL).

When selection = away - return APLin the new column.

When selection = Draw - return APL in the new column

I already did the first example(Home_APL), however when I run the same IF function for Away_APL it doesn't work.

#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
  • 141,215
  • 6
  • 77
  • 149
Rama
  • 25
  • 4
  • Please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This specifically (in my opinion) excludes the use of images of data: (1) you are asking us to transcribe data from an image into something useful, *ain't gonna happen*; (2) as is, it does not show the actual class of your data (e.g., `factor`, `numeric`, `character`, `POSIXt`); (3) it is just as easy for you to do `dput(head(x,n=10))` and paste that as it is to paste an image. Seriously, please read the link then edit your question. – r2evans Jul 05 '18 at 15:49
  • Here is the .csv file: https://github.com/seyfuram/dataset/blob/master/NAsss.csv – Rama Jul 05 '18 at 16:29
  • Data in comments is horribly difficult to do well. Links to external files (either in a comment or the question) often go bad, rendering the question unreproducible. Can you provide sample data within the question itself? – r2evans Jul 05 '18 at 16:31

1 Answers1

0

Like @r2evans said, make your question reproducible. And specify what was the error when you say 'it doesn't work'.

The reason why its failing might be because you are adding new columns with just some of the rows populated. So R doesn't know how to fill the missing rows and you will get a

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

error.

To tackle this, you can create both columns manually first with a value and then populate the matching rows.

pre_df$Home_APL <- 0
pre_df$Away_APL <- 0

Then run your loop,

# > 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