[R] Extracting portions of one vector to create others

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue Sep 2 09:50:53 CEST 2025


On 9/2/2025 8:38 AM, Rui Barradas wrote:
> On 9/1/2025 3:09 PM, Paul Zachos 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.
> Hello,
> 
> Here is a way. This creates a matrix with the vectors you ask for.
> But it doesn't create 5 different vectors, it keeps them in one object 
> only, a matrix.
> 
> 
> 
> CLASS <-
>    c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>      1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
>      2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L,
>      4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 7L, 7L,
>      7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
>      7L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 1L)
> Beth <- data.frame(CLASS)
> 
> eq <- c(1, 2, 4, 7, 9)
> res <- sapply(eq, \(x) as.integer(x == Beth$CLASS))
> colnames(res) <- paste0("CLASS", eq)
> head(res)
> #>      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
> 
> 
> If you really want 5 different objects in the global environment, you 
> can use ?list2env.
> This is not a good practice, you will have related, loose objects in the 
> globalenv, making your code harder to debug. Keep it simple.
> 
> 
> as.data.frame(res) |>
>    list2env(envir = .GlobalEnv)
> #> <environment: R_GlobalEnv>
> 
> 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
> #> [39] 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
> #> [77] 0 0 0 0 0 0 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
> #> [39] 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
> #> [77] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> ______________________________________________
> 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.
Hello,

Here is another way with ?model.matrix.


res2 <- model.matrix(~ 0 + factor(CLASS), data = Beth)
colnames(res2) <- sub("factor\\((.*)\\)", "\\1", colnames(res2))



This will create matrix res2 with other attributes, not just dimnames. 
To get rid of those you can run


attr(res2, "assign") <- NULL
attr(res2, "contrasts") <- NULL
attributes(res2)


Hope this helps,

Rui Barradas



More information about the R-help mailing list