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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:31:44+00:00 2026-06-07T21:31:44+00:00

I have a termDocumentMatrix created using the tm package in R. I’m trying to

  • 0

I have a termDocumentMatrix created using the tm package in R.

I’m trying to create a matrix/dataframe that has the 50 most frequently occurring terms.

When I try to convert to a matrix I get this error:

> ap.m <- as.matrix(mydata.dtm)
Error: cannot allocate vector of size 2.0 Gb

So I tried converting to sparse matrices using Matrix package:

> A <- as(mydata.dtm, "sparseMatrix") 
Error in as(from, "CsparseMatrix") : 
  no method or default for coercing "TermDocumentMatrix" to "CsparseMatrix"
> B <- Matrix(mydata.dtm, sparse = TRUE)
Error in asMethod(object) : invalid class 'NA' to dup_mMatrix_as_geMatrix

I’ve tried accessing the different parts of the tdm using:

> freqy1 <- data.frame(term1 = findFreqTerms(mydata.dtm, lowfreq=165))
> mydata.dtm[mydata.dtm$ Terms %in% freqy1$term1,]
Error in seq_len(nr) : argument must be coercible to non-negative integer

Here’s some other info:

> str(mydata.dtm)
List of 6
 $ i       : int [1:430206] 377 468 725 3067 3906 4150 4393 5188 5793 6665 ...
 $ j       : int [1:430206] 1 1 1 1 1 1 1 1 1 1 ...
 $ v       : num [1:430206] 1 1 1 1 1 1 1 1 2 3 ...
 $ nrow    : int 15643
 $ ncol    : int 17207
 $ dimnames:List of 2
  ..$ Terms: chr [1:15643] "000" "0mm" "100" "1000" ...
  ..$ Docs : chr [1:17207] "1" "2" "3" "4" ...
 - attr(*, "class")= chr [1:2] "TermDocumentMatrix" "simple_triplet_matrix"
 - attr(*, "Weighting")= chr [1:2] "term frequency" "tf"
> mydata.dtm
A term-document matrix (15643 terms, 17207 documents)

Non-/sparse entries: 430206/268738895
Sparsity           : 100%
Maximal term length: 54 
Weighting          : term frequency (tf)

My ideal output is something like this:

term      frequency
the         2123
and         2095
able         883
...          ...

Any suggestions?

  • 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-07T21:31:46+00:00Added an answer on June 7, 2026 at 9:31 pm

    The term-document matrices in tm are already created as sparse matrices. Here, mydata.tdm$i and mydata.tdm$j are the vectors of indexes of the matrix and mydata.tdm$v is the related vector of frequencies. So that you can create a sparse matrix writing :

    sparseMatrix(i=mydata.tdm$i, j=mydata.tdm$j, x=mydata.tdm$v)
    

    Then you can use rowSums and link the rows, you’re interested in, to the terms, they stand for, with $Terms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a matrix report now that has Position, Hours and Wages for a location
I am using the tm package to compute term-document-matrix for a dataset, I now
Have a simple contact us XPage created. Have server side validation in place that
Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',
have a nice day. I got problem when trying to create an image from
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
Have you ever created or encountered a self modifying code in Java? If yes,
Have you ever had to justify the choice over using .NET instead of Java
Have a bunch of WCF REST services hosted on Azure that access a SQL

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.