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?