[R] Comparing values of different columns: Error: level sets of factors are different

Chichi Shu chichi.shu at hotmail.com
Fri Oct 17 22:21:27 CEST 2014


Hi, R listers,
I¡¯m trying to compare a value of a row in a column to values of previous rows in another column in a loop. ¡°i¡± is just from first row to the last row. j is an another looping controller indicating the rows that row[i] will be compared to and j will be rows before row[i]. I want to compare phone_1[i] with phone_2[j] and vise versa.
for (i in 1: nrow(df)) {
  if (df$Incremental[i] == 1) {mark[i] <- 1}
    else {for (j in (i ¨C df$Incremental[i] + 1) : (i - 1)) {
      if ((df$Phone_1[i] != "" & df$Phone_1[i] == df$Phone_2[j]) | (df$Phone_2[i] != "" & df$Phone_2[i] == df$Phone_1[j])) {
        mark[i] <- mark[j]}
          else {mark[i] <- mark[i-1] + 1}
                                                                                   }
}
However I¡¯m getting an error with phone_1[i] and phone_2[j], indicating that ¡°level sets of factors are different¡±. I¡¯m not sure what I need to do to fix it.
Thanks!
	[[alternative HTML version deleted]]



More information about the R-help mailing list