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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:53:25+00:00 2026-06-18T12:53:25+00:00

I think this is a beginner question, but I don’t appear to have the

  • 0

I think this is a beginner question, but I don’t appear to have the right vocabulary for an effective Google search.

I have a data.frame, final, which contains a list of clusters, each of which is a list of strings.

I would like to iterate over the list of strings in each cluster: a for loop within a for loop.

for (j in final$clusters){
    for (i in final$clusters$`j`){
        print final$clusters$`j`[i]
    }
}

j corresponds to the lists in clusters, and i corresponds to the items in clusters[j]

I was trying to do this by using the length of each cluster, which I thought would be something like length(final$clusters[1]), but that gives 1, not the length of list.

Also, final$clusters[1] gives $’1′, and on the next line, all the strings in cluster 1.

Thanks.

EDIT: output of dput(str(final)), as requested:

List of 2
 $ clusters     :List of 1629
  ..$ 1   :
  ..$ 2   : 
  ..$ 3   : 
  ..$ 4   : 
  ..$ 5   : 
  ..$ 6   : 
  ..$ 7   : 
  ..$ 8   : 
  ..$ 9   : 
  ..$ 10  : 
  .. [list output truncated]
 $ cluster_stats: num [1:1629, 1:6] 0.7 0.7 0.7 0.7 0.7 0.7 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:1629] "1" "2" "3" "4" ...
  .. ..$ : chr [1:6] "min" "qu1" "median" "mean" ...
NULL
  • 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-18T12:53:26+00:00Added an answer on June 18, 2026 at 12:53 pm

    I think you confuse a list and a data.frame. I guess that your final is object is a list.

    To iterate over the list You can use rapply. It is a recursive version of lapply.

    For example:

    ## I create some reproducible example
    
    cluster1 <- list(a='a',b='b')
    cluster2 <- list(c='aaa',d='bbb')
    clusters <- list(cluster1,cluster2)
    final <- list(clusters)
    

    So using rapply

    rapply(final,f=print)
    [1] "a"
    [1] "b"
    [1] "aaa"
    [1] "bbb"
        a     b     c     d 
      "a"   "b" "aaa" "bbb" 
    

    Update after edit by OP

    Using lapply, I loop through the name of the list. For each name, I get the element list using [[ ( you can use [ if you wand to get names and heder for files), then I write the file using write.table. Here I use the name of the element in the list to create the file name. in your case you will have file name as number.(1.txt,…)

        lapply(names(final$clusters),
                          function(x)
                                 write.table(x=final$clusters[[x]],
                                             file=paste(x,'.txt',sep='')))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this could be a very easy question for you. But I have
This seems like a very beginner question, but I don't know the answer. Suppose
(I think this is a pretty basic question on OOP, but unfortunately I wasn't
I think this is a simple and a silly question. I have included a
I think this is mostly because I'm new to PHP OOP, but I have
I think this question has been asked many a times but I've been searching
I'm sorry that I put this question because I think that is simple but
Okay, this might be a very silly beginner question, but: I've got an ClassA,
I think this is a beginner question, it is regarding the variable references across
This question may be simple for experts but for a beginner like me it

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.