Generate dummy variable with multiple levels in R -


my question involves how generate dummy-variable character variable multiple repeated characters in r. number of times character repeated varies. there several questions topic, none of them seem address specific problem. below minimal example of data:

df <- data.frame(id=c("c/004","c/004","c/005","c/005","c/005","c/007",                      "c/007", "c/007")) 

the result expect follows:

  > df          id newid     1 c/004     1     2 c/004     1     3 c/005     2     4 c/005     2     5 c/005     2     6 c/007     3     7 c/007     3     8 c/007     3 

i have resulting variable newid of numeric class , not factor , not go function factor(.., levels=...)

since results factor variable , besides required supply factor levels many.

any assistance appreciated.

you can in couple of ways

match(df$id, unique(df$id)) #[1] 1 1 2 2 2 3 3 3 

or

as.numeric(factor(df$id)) #[1] 1 1 2 2 2 3 3 3 

or

cumsum(!duplicated(df$id)) #[1] 1 1 2 2 2 3 3 3 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -