mardi 24 mai 2016

How to check if values in two columns has the same class in R?

I have to check if a value in two columns or lists has the same class. I wrote the following codes but none are working because just write the last value on the list but not only the first values.

My lists

x <- c(1,3,6,2) ## All are numeric
y <- c(6,4,3,'a') ## Note that is a string at here
m <- NULL

Code 1

for (i in length(x)) {
  if (class(x[i]) == class(y[i])) m[i] <-'ok' else m[i] <- 'no ok'
}

Code 2

et <-function(x, y){
  for (i in length(x)) {
    if (class(x[i]) == class(y[i])) {
      m[i] = 'ok'
    } else {
      m[i] = 'not ok'
    }
    return(f)
  }
}
et(x,y)

Thanks for helping.

Aucun commentaire:

Enregistrer un commentaire