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

c++ - CPP, 'X' button listener -

shared memory - gstreamer shmsrc and shmsink with h264 data -

.net - Bulk insert via Dapper is slower than inserting rows one-by-one -