[R] Extracting portions of one vector to create others
Jeffrey Dick
j3||d|ck @end|ng |rom gm@||@com
Tue Sep 2 09:07:20 CEST 2025
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 <paz using 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 |
> paz using acase.org | www.acase.org
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using 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.
More information about the R-help
mailing list