I hope nobody is getting (too) tired of me. I have given a completely fictional example of the problem below. The error is only given after including a mixture of numeric and string column in a data frame, even if the numeric columns are excluded from the function (not shown below). The function looks in column c for contents of d and vice-versa using grepl and then the should make a new column of the 'yes' and 'no' statements from ifelse. I need to ifelse to return the 'yes' and 'no' after the test but this is the part that is giving the error.
a <- c(5:10)
b <- c(105:110)
c <- c("a","b","c","d","e","f")
d <- c("aa","bc","cd","ff","ee", "gf")
df <- data.frame(a,b,c,d)
newfunction <- function(x, col1, col2, col3, col4){ifelse(((sapply(lapply(x[[col4]], grepl,
x[[col3]]),any)) | (sapply(lapply(x[[col3]], grepl, x[[col4]]),any))), (11 - x[[col1]]), (1 -
x[[col2]]))}
df$new <- apply(df, 1, newfunction, "a", "b", "c", "d")
Error in 11 - x[[col1]] : non-numeric argument to binary operator
Aucun commentaire:
Enregistrer un commentaire