Question

In my dataset I want to create dummy variables for identifying the influence of a variable within certain distances.

The distances should be:

0-100 meters

100-200 meters

200-300 meters

up to 1000 meters so all in all 10 dummy variables.

How do I do this

Thank you in advance

EDIT:

When I run a regression with the dummy variables, I can't get the interval 0-100 meters in the regression. It's left out. Any ideas on why?

Thanks in advance

Was it helpful?

Solution

Are you looking for cut function:

x <- 1:1000
cut(x,c(min(x),100,200,300,max(x)))

EDIT

To get the 10 levels :

cut(x,seq(min(x),max(x),100))

OTHER TIPS

You can also use recode function from car package in R.

install.packages("car")
library(car)
x<-0:1000
y<-recode(x,"0:100=1;101:200=2;201:300=3;301:400=4;401:500=5;501:600=6;601:700=7;701-800=8;801:900=9;else=10")

Output: 

> head(y)
[1] 1 1 1 1 1 1
> tail(y)
[1] 10 10 10 10 10 10

Note: You can manipulate the min and max for each category. For example, instead of 100 for max in the first category, you can write 99.999 depending upon your data structure.

There is also ifelse function which you can use if you have only few categories. See here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top