Jim
Jim

Reputation: 55

Making a continuous variable categorical in R

I have the variable YEAR which takes 17 consecutive values from 1925 to 1941. is.numeric is showing it to be a numeric variable. I want to turn it into a categorical variable with 17 levels (so each level has 1 value assigned to it). I tried the code

g <- cut(data$YEAR, breaks=c(1925, 1941, 1))

but it shows this error

Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
  path[1]="":

and the table(g) command gives me

 (1,1.92e+03] (1.92e+03,1.94e+03] 
                  1                  16

I think my request is fairly simple, I don't know why R does this. Can someone help?

EDIT: The values of the variable YEAR are: 1925, 1926, 1927, 1928, 1929, 1930, 1931, 1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1940, 1941.

Upvotes: 0

Views: 840

Answers (1)

Jonathan
Jonathan

Reputation: 1088

You convert the numeric values to factors with as.factor, so what you probably want to do is

g <- as.factor(data$YEAR)

Upvotes: 1

Related Questions