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

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -