Your request is equivalent to "how to do one-hot encoding in R". You can do this search yourself.
Just for fun, I did that search and the following is a short solution: A <- model.matrix( ~ as.character(Beth$CLASS) - 1 ) colnames(A) <- paste0("CLASS", c(1,2,4,7,9)) A is now a matrix whose columns are the vectors that you want. To see this: head(A) CLASS1 CLASS2 CLASS4 CLASS7 CLASS9 1 1 0 0 0 0 2 1 0 0 0 0 3 1 0 0 0 0 4 1 0 0 0 0 5 1 0 0 0 0 6 1 0 0 0 0 On Tue, Sep 2, 2025 at 10:07 AM Jeffrey Dick <j3ffd...@gmail.com> wrote: > Hi Paul, > > Here's a way that creates new vectors as elements of a list. > > Beth <- list( > 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 > ) > ) > > classes <- unique(Beth$CLASS) > > # Loop over `classes` to operate on a single `class` in each iteration > result <- lapply(classes, function(class) { > # Create a vector with 1 if Beth$CLASS is in class, 0 if not > as.numeric(Beth$CLASS == class) > }) > > names(result) <- paste0("CLASS", classes) > > with the result: > > > result > $CLASS1 > [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 0 0 0 0 0 > 0 > [83] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 > > $CLASS2 > [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 > 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 0 0 0 0 0 > 0 > [83] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 > > [list truncated ...] > > Regards, > Jeff > > On Tue, Sep 2, 2025 at 2:44 PM 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. > > ______________________________________________ > 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. > [[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.