for loop for values in table function plus concatenating to data frame

Hi all,

I am having . problem i would very much appreciate some help with! It involves a for loop on two levels (explained below) and then concatenating the individual results from each iteration of 'i' in to one data frame..

I have an R object within which I have some metadata. I want to access some stats using the table function (which works fine).. The call looks like this:

table(object@metadata$cluster== 2 & object@metadata$filename ==1)

This works fine and give me a nice breakdown of how many elements from cluster 2 belong to filename The problem is the length of clusters and filename. It would be too time consuming to this many times.

cluster length = 20

What i would like to do is generate a for loop that gives me the statistics above by iterating through elements of i and the concatenate these individual results in to one dataframe.

I am comfortable using for loops, however I have never been in a situation where I need to iterate through two elements at the same time:


for (i in cluster) {

x<- table(object@metadata$cluster== i & (object@metadata$filename == **????**)


I am not sure how to implement this loop to go through i and "j" which would be after file name...

What i would then like to do is concatenate all the results in to one data frame similar to:



with the names of i in the cluster and filename as the titles to the respective columns..

any help would be greatly appreciated!

many thanks!!

I am not understanding exactly what you want to accomplish. To answer the question of how to iterate over two indices:

for (i in clusters) {
  for (j in filename) {

    x<- table(object@metadata$cluster== i & (object@metadata$filename == j)
    # or
    #print(paste(i, j, x)

However, I think you would get the same information from

table(object@metadata$cluster, object@metadata$filename)

Perhaps I am not understanding the structure of your data. Could you show what object@meatadata looks like?

thank you so much!!! this actually answer the question. the '==' was producing a logical answer, but

table(object@metadata$cluster, object@metadata$filename)

is a perfect solution!

thank you for clarifying on how to iterate through two indices as well!!

'Perhaps I am not understanding the structure of your data'

you answered my question perfectly, thanks!

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.