1

I'm working with a dataframe in R. One of the columns is a character vector where each value is either an empty "" or of the form "X - Y" where X and Y are two numbers representing the score of a football game. I'd like to add two new numeric vectors to my dataframe, one column consisting of all the X numeric values and the other consisting of all the Y numeric values. I'm currently trying to create the column of X value's like so:

create_away_score_column <- function(data) {
  return (as.numeric(sapply(strsplit(data$score, " - "), "[[", 1)))
}

When I try to use this function, calling it as data$away_score <- create_away_score_column(data), I get a subscript out of bounds error. Any idea why I am accessing something out of bounds?

  • Have a look [here](https://stackoverflow.com/questions/7069076/split-column-at-delimiter-in-data-frame) and [here](https://stackoverflow.com/questions/4350440/split-a-column-of-a-data-frame-to-multiple-columns), should be helpful. – Ronak Shah Jul 03 '17 at 02:20

0 Answers0