undefined columns selected

Hello,

I don't know much about r language. So that's why I am asking your help on this, if you can.
I took a code available on internet that I tried to run.
I had a look on the forum but I still struggling.
So here below the error message:

Error in `[.data.frame`(df, row_idx, col_names) : 
  undefined columns selected

A code part related to the error message:

row_max_prob <- function(df, row_idx, find_max){
  predict_outcome <- df[row_idx, "predict_outcome"]
  if(is.na(predict_outcome)) return (NA)
  col_names <- paste0(betting_house, predict_outcome)
  names(df)
  val = ifelse(find_max == 1, max(df[row_idx,col_names]), 1 / mean(as.numeric(df[row_idx,col_names])))
  return ( val )
}

Some help could be great.

My hope is that by running this code, you will undestand something of R syntax, and the sort of problem that causes the error you have.

#iris is a built in example dataset all R users have
#head function prints the first few records of it
(head(iris))
#nrow tells the names of the columns in it
(names(iris))

#look at the first row
(iris[1,])

#look at the first row but on the Sepal.Length
(iris[1,"Sepal.Length"])

#look at the first row but for a column that isnt there
(iris[1,"xyz"])

#look at the all the rows for Sepal Length
(iris[,"Sepal.Length"])

#look at the all the rows but for a column that isnt there
(iris[,"xyz"])

it seems like col_names might not refer to an actual column in your data. (betting_house seems weird in the function definition), also that row_idx might not be populate with relevant values.

Hello,
Normally the code is like that

betting_house <- c("B365", "BW", "IW", "PS", "WH", "VC")

row_max_prob <- function(df, row_idx, find_max){
predict_outcome = df[row_idx, "predict_outcome"]
if (is.na(predict_outcome)) return (NA)
col_names <- paste0(betting_house, predict_outcome)
val = ifelse(find_max, max(df[row_idx,col_names]), 1/mean(as.numeric(df[row_idx,col_names])))
return (val)
}

It was a little mistake in the previous code. Seems to me row_idx should be number value so how can I populate it?

Thanks as well for you answer.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.