Wednesday, 15 September 2010

if statement - Ifelse in R or apply -



if statement - Ifelse in R or apply -

i attempting create elements in first column of df null (no entry @ all) if equal element in same row in sec column. simple thing, haven't been able find reply in message boards.

below 2 of attempts:

ifelse(y2014[y2014[,1]==y2014[,2]],y2014[,1]=="",y2014[,1]==y2014[,1]) y2014$new=ifelse(y2014[,1]==y2014[,2],0,y2014[,1])

both give next error: "level sets of factors different" checked number of levels in each , they're equal, though several cells blank in column 2. apply function work improve i'm trying accomplish?

really appreciate help newbie.

two things, factors need converted character prior comparing, , want assign na rather 0 value.

something might better:

y2014$new <- y2014[,1] y2014$new[as.character(y2014$new) == as.character(y2014[,2])] <- na

r if-statement

No comments:

Post a Comment