jim holtman:
> You might want to look at how to use 'lapply' to create lists.  Here
> is one way of doing it:
>  
> > # create test data
> > a_threshold <- b_threshold <- as.data.frame(matrix(sample(c(1:5,
> NA), 100, TRUE), 10))
> > classification <- c('a', 'b')
> > result <- lapply(classification, function(.cls){
> +     colSums(!is.na(get(paste(.cls, '_threshold', sep=''))))
> + })


Jim, thank you for your time. You saved my day ;-).

Just for your interest, my code (even if not elegant) works also with
"colSums()". Great function. Of course, the lapply() is probably the
elegant solution to many tasks like mine.

Kindest regards, Nikos


> The code 
# loop over "classifications"
for (x in classifications) {

 # loop over sequence 1 to 10
 for (i in 1:10)

  # store sum's per "source" column
  assign ( (paste ( x, "_sums", sep = "" )[i],

    # changed from sum to colSums
    colSums ( !is.na (
    get ( paste ( x, "_thresholds", sep = "" ) ) # removed the "[ ,i]" #
   )
  )
 )
}

The 2nd question remains or it is again solved with an _apply()_
function. Maybe I'll find the answer in R_inferno?

>         Questions
--%<---
>         2. Related question: how can I print the structure of each
>         column of each "classification" with a for loop?
>         e.g.

>         # a single loop work perfectly as follows:
>         for (i in 1:10)
>         str(burned_eig_normalised_cov.omission_thresholds[,i])
>         
>         int [1:875] NA NA NA NA NA NA NA NA NA NA ...
>          int [1:875] NA NA NA NA NA NA NA NA NA NA ...
>          int [1:875] NA NA NA NA NA NA NA NA NA NA ...
>          int [1:875] NA NA NA NA NA NA NA NA NA NA ...
>          int [1:875] NA NA NA NA NA NA NA NA NA NA ...
>          int [1:875] NA NA NA NA NA NA NA NA NA NA ...
>          int [1:875] NA NA NA NA NA NA NA NA NA NA ...
>          int [1:875] NA NA NA NA NA NA NA NA NA NA ...
>          int [1:875] NA NA NA NA NA NA NA NA NA NA ...
>          logi [1:875] NA NA NA NA NA NA ...
>         
>         # now, I would like to say
>         for (x in classifications) { for (i in 1:10) str(paste(x,
>         ".omission_thresholds", sep="")[,i]) }
>         
>         Error in paste(x, ".omission_thresholds", sep = "")[, i] :
>          incorrect number of dimensions
>         
>         Why is this wrong? Given the common prefix, how can I "paste"
>         the
>         "prefix and the suffix" and access the columns?

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to