This is an extremely common transformation in regression, and it has a 
dedicated function (model.matrix) for accomplishing it in R. It does presume 
you have a bit of familiarity with the formula literal type in R... which is 
created using the tilde character (e.g. ~ CLASS). Also, since including an 
intercept term in a regression is the default, if you only want the columns you 
described then you need to inform the function that you want to leave the 
intercept out (~ CLASS - 1). It doesn't apply this discrete transformation 
unless the referenced variable is a factor variable, so I convert it from 
integer type in the short example below.

Beth <- data.frame(

  CLASS = c(
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2
    , 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4
    , 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
    , 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1))
Beth$CLASS <- as.character(Beth$CLASS)
result <- model.matrix( ~ CLASS - 1, Beth)
result

Note that the result is a matrix, not a data frame so you will have to use 
brackets and a blank for the row spec if you want to access one column at a 
time: 

result[, "CLASS1"]

Refer to the help page for more on this function:
?model.matrix



On September 1, 2025 7:09:56 AM PDT, Paul Zachos <p...@acase.org> wrote:
>Dear Colleagues,
>
>I have a vector which indicates membership of subjects in one of 5 Classes
>
>Beth$CLASS
> [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
>[37] 2 2 2 2 2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 7 7 7 7 7 7 7 7 7 7 7
>[73] 7 7 7 7 7 7 7 7 7 9 9 9 9 9 9 9 9 9 9 9 9 9 9 1
>
>For purposes of an analysis (using linear models based on Ward and Jennings) I 
>would like to create 5 new vectors
>
>The values in vector CLASS1 will be ‘1’ if the corresponding value in 
>Beth$CLASS is equal to ‘1’; ‘0’ otherwise
>
>The values in vector CLASS2 will be ‘1’ if the corresponding value in 
>Beth$CLASS is equal to ‘2’; ‘0’ otherwise
>
>The values in vector CLASS4 will be ‘1’ if the corresponding value in 
>Beth$CLASS is equal to ‘4’; ‘0’ otherwise
>
>The values in vector CLASS7 will be ‘1’ if the corresponding value in 
>Beth$CLASS is equal to ‘7’; ‘0’ otherwise
>
>The values in vector CLASS9 will be ‘1’ if the corresponding value in 
>Beth$CLASS is equal to ‘9’; ‘0’ otherwise
>
>How would I go about this using R
>
>Thank you
>_________________
>Paul Zachos, PhD
>Director, Research and Evaluation
>Association for the Cooperative Advancement of Science and Education (ACASE)
>110 Spring Street  Saratoga Springs, NY 12866  |  
>p...@acase.org  |  www.acase.org
>
>
>
>
>
>       [[alternative HTML version deleted]]
>
>______________________________________________
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to