I have a data frame T that is a mixture of numeric and string:
T1<-c(1,2, 3,4,6)
T2<-c(4,5, 5,7,8)
T3<-c("a","b","c","d","e")
T4<-c(4,5, 5,7,8)
T5<-c(4,5, 5,7,8)
T<-data.frame(T1,T2,T3,T4,T5)
When I apply function to the numeric value of each row using follwong code:
P=apply(T,1,FUN=function(x) ifelse(x[1]>=x[4]+2*x[5],1,0))
It always give error message "Error in 2 * x[5] : non-numeric argument to binary operator". But if I replace T3 with all numeric values, it works perfectly.
I am puzzled by this and wondering anyone has any insight?
thanks!
apply
to adata.frame
.apply
is meant forarray
objects and so it convertsT
to amatrix
. Since yourdata.frame
containscharacter
column, it will be coerced to acharacter
matrix and so arithmetic operations are not allowed. – nicola(T[,1]>=T[,4]+2*T[,5])+0
– akrun