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 7994697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:12:00+00:00 2026-06-04T14:12:00+00:00

My problem is the following: I need to reduce a matrix cutting some columns

  • 0

My problem is the following:

I need to reduce a matrix cutting some columns away but keeping the names of column vectors.
DTM is my original matrix that looks like the following:

>DTM
   word1    word2    word3    word4
[1] 1         1        0        0
[2] 2         0        1        1
[3] 0         1        0        2

and I want to obtain a new matrix (DTMr in the following chunk of code) that has ‘labels’ and eliminates all columns whose sum of members is less than a threshold (say 2):

   word1    word4
[1] 1         0
[2] 2         1
[3] 0         2

>DTMr <- matrix(,nrow=nrow(DTM),ncol=d) # This should be the reduced matrix

where d is the number of columns of DTM that are larger than the threshold

>c = 1 # new counter
>for (col in 1:ncol(DTM))
>{
>  if (sum(DTM[,col]) > 2) 
>  { 
>    DTMr[,c] = DTM[,col]
>    
>    c=c+1
>  }
>}

Unfortunately in this way, DTMr is perfect, but it loses all labels (word 1, …word n).

Any ideas?

Claudio

  • 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-04T14:12:01+00:00Added an answer on June 4, 2026 at 2:12 pm

    A simple solution using subsetting and colSums:

    Create some sample data:

    set.seed(1)
    x <- matrix(sample(0:2, 12, replace=TRUE), ncol=4)
    colnames(x) <- LETTERS[1:4]
    x
         A B C D
    [1,] 0 2 2 0
    [2,] 1 0 1 0
    [3,] 1 2 1 0
    

    Subset:

    x[, colSums(x)<4]
         A D
    [1,] 0 0
    [2,] 1 0
    [3,] 1 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem: I need to use XSLFO to generate a 2-column
i have the following problem: i need to send some custom info with every
I would really appreciate any help with the following problem: I need to be
I need a regular expression to solve the following problem (links to similar problems
I'm facing the following problem: in the controller I select the data I need
While making AutoHotkey-script I encountered the following problem. I need navigate listbox (one position
I have a problem with Matlab - I need to do the following :
Im stumbling upon a problem where i need to retrieve data from the following
I have the following problem: I need to get a ConfigurationSection - object from
I have the following problem: I need to insert N rows after row X.

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.