Re: Extracting portions of one vector to create others

"Richard O'Keefe" <[email protected]>
Newsgroups gmane.comp.lang.r.general
Message-ID <CABcYAdJ5VW1pWV4B0b7g-xwUgjD1tzp2Qdyg=V3sbMVkRtbCGg@mail.gmail.com>
To do exactly what you asked for,
CLASS1 <- rep('0', len=length(Beth$CLASS))
because there is NO element in Beth$CLASS that is '1'.
Assuming that you did not mean ANY of the single quotes to be taken seriously,
CLASS1 <- as.integer(Beth$CLASS == 1)
Beth$CLASS == 1 will give you TRUE where Beth$CLASS has 1, FALSE elsewhere.
as.integer(Beth$CLASS == 1) will convert TRUE to 1 and FALSE to 0.
If you really want those 1s and 0x to be '1's and '0's,
as.character(as.integer(Beth$CLASS == 1))
will do the trick.  What you might find to be clearer is
CLASS2 <- ifelse(Beth$CLASS == 2, '1', '0')

For linear modelling, you are probably interested in factors rather
than strings.
To avoid confusion, you might want to avoid using '0' and '1'

On Tue, 2 Sept 2025 at 18:44, Paul Zachos <[email protected]> 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  |
> [email protected]  |  www.acase.org
>
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [email protected] 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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.