简体   繁体   中英

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.

您使用as.factor将数值转换为因子,所以您可能想要做的是

g <- as.factor(data$YEAR)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM