Skip to content
Prev 398122 / 398506 Next

Extracting portions of one vector to create others

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 <paz at acase.org> wrote: