On Oct 17, 2014, at 1:21 PM, Chichi Shu wrote:

> 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.

Factor fixing. An ancient, aRcane sport.

Perhaps:
lev2 <- unique( c( levels(phone_1), levels(phone_2) ) )
phone_1 <- factor(phone_1, levels=lev2)
phone_2 <- factor(phone_2, levels=lev2)




> Thanks!
>       [[alternative HTML version deleted]]

And HTML posting is not well supported. Learn to post in that most ancient of 
computer tongues, plain text.

> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 

David Winsemius
Alameda, CA, USA

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to