I have a column of factors that I want to convert to characters. (e.g. '18-DEC-15'). Calling df$dates
and unique(df$dates)
yields a typically diverse column of dates/factors, as expected.
Calling as.character(df$dates)
yields the same list as df$dates
, visually inspected from the console. However, calling ifelse(TRUE,as.character(df$dates),df$dates)
yields only a single element - a single, arbitrary date.
I've no clue why wrapping a call in ifelse would change the underlying operation. Dplyr seems to solve this problem, as calling transmute(df,newvar=as.character(dates))
does produce the correct list, unlike the maddeningly simple ifelse(). This seems to be a huge argument in favor of mutate I've never heard before. Also, ifelse(FALSE,as.character(df$dates),df$dates)
returns a single integer (~130) rather than the actual list of factors contained in df$dates
Edit - thanks for helpful comments. rep(T/F,nrow()) is precisely the right approach and explains why the first test failed (ie, it actually would have passed). The question is now the difference between ifelse(rep(TRUE,nrow(df)),as.character(df$dates),df$dates)
and ifelse(rep(FALSE,nrow(df)),as.character(df$dates),df$dates)
. Obviously, making the correction, the FALSE test still fails. namely, it returns a vector of integers while the TRUE test returns the proper vector (one of readable dates)