statistics - How to define a factor as being above or below its median in R? -


so need create factor defines 'alt' variable (altitude) being above or below median.

i know should follow basic shape of creating new:

conservation$alt.factor <- conservation$alt (..????...) 

but i'm struggling last bit.

there must simple command out there!

use ifelse():

conservation$alt.factor <- factor(   ifelse(conservation$alt < median(conservation$alt, na.rm = true), 1,     ifelse(conservation$alt > median(conservation$alt, na.rm = true), 2, na)),   1:2, labels = c("below", "above")) 

beware, setup sets rows conservation$alt == median(conservation$alt) na. if not wanted, change last parameter in ifelse-statement else.


Comments

Popular posts from this blog

qt - QML MouseArea onWheel event not working properly when inside QML Scrollview -

java - is not an enclosing class / new Intent Cannot Resolve Constructor -

python - Error importing VideoFileClip from moviepy : AttributeError: 'PermissionError' object has no attribute 'message' -