For loops, with replacement in R -
i have dataframe factors.
dat <- c("low", "high", "middle", "middle", "low", "low") dat <- factor(dat, levels = c("low", "middle", "high") lvl <- unique(sort(dat)) x <- seq(1,3,1)
i want iterate through each factor , replace factors numeric variables if equals ith level.
for(i in 1:3){ if(dat == lvl[i]) { dat <- x[i] } }
i keep getting error: the condition has length > 1 , first element used
.
there must simple solution? edit clarity:
i'm not trying replace vector corresponding level. i'm trying replace entry term in sequence (1,2,3 in case) corresponds level.
you use ifelse particular task can use as.numeric.
dat <- as.numeric(dat)
Comments
Post a Comment