Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8028291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:05:45+00:00 2026-06-05T00:05:45+00:00

Given the matrix test <- structure(list(X1 = c(3L, 0L, 3L, 1L, 2L, 2L, 1L,

  • 0

Given the matrix

test <- structure(list(X1 = c(3L, 0L, 3L, 1L, 2L, 2L, 1L, 2L, 2L, 3L), 
X2 = c(2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), X3 = c(0L, 
0L, 3L, 0L, 2L, 2L, 3L, 0L, 0L, 2L), X4 = c(1L, 1L, 1L, 0L, 
3L, 1L, 3L, 1L, 1L, 1L), X5 = c(3L, 3L, 1L, 3L, 1L, 3L, 2L, 
3L, 3L, 3L), X6 = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
), X7 = c(2L, 2L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 2L), X8 = c(3L, 
0L, 1L, 0L, 1L, 1L, 3L, 0L, 0L, 1L), X9 = c(3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L)), .Names = c("X1", "X2", "X3", "X4", 
"X5", "X6", "X7", "X8", "X9"), row.names = c("1", "2", "3", "4", 
"5", "6", "7", "8", "9", "10"), class = "data.frame")

I’m trying to replace each instance of "1" by the sequence of numbers c(0,0,1), 2 by c(0,1,0), 3 by (1,0,1), and 0 by (0,0,0). Each value in the matrix should be replaced with one of these sequences of three binary values. The nrow of the resulting matrix should be nrow(test) * 3. Obviously I tried to use indexes test[test == 1] <- c(0,0,1) but this returns error rhs is the wrong length for indexing by a logical matrix. The replace function also doesn’t seem to work here, returning the same error message. Any idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:05:46+00:00Added an answer on June 5, 2026 at 12:05 am

    Since the dimension of the result is different (a 3-dimensional array instead of a 2-dimensional matrix), you cannot just replace the elements one by one.

    You can use apply: if is often used to apply a function to each column or row of a matrix, but we can also use it to apply a function to each element.

    apply( 
      test, 
      1:2, 
      function(u) list(c(0,0,0), c(0,0,1), c(0,1,0), c(1,0,1))[[u+1]] 
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the matrix structure(list(X1 = c(1L, 2L, 3L, 4L, 2L, 5L), X2 = c(2L,
Problem: Given a matrix in which each row and each column is sorted, write
Given this definition and a test matrix: data (Eq a, Show a) => QT
Given the matrix, df <- read.table(text=" X1 X2 X3 X4 X5 1 2 3
Circumstances I have a procedure which will construct a matrix using the given list
In NumPy, I'm trying to use linalg to compute matrix inverses at each step
I have two method : template <class T> Matrix<T>::Matrix(int rows, int cols, T* data)
I have a function that returns a comparison matrix from a given list: def
I am trying to write a quadtree sparse matrix class. In short, a quadtree_matrix<T>
If I have a matrix given as a list of rows [[1,2,3],[4,5,6]], I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.