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=''))))
+ })>
> result
[[1]]
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
9 9 9 9 8 7 7 8 9 8
[[2]]
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
9 9 9 9 8 7 7 8 9 8>
On Sat, May 23, 2009 at 10:32 AM, Nikos Alexandris <
nikos.alexandris@felis.uni-freiburg.de> wrote:
> Hi R-list.
>
> This is my first post. I'll try to be as precise as possible with the
> difficulty I have to "get things done".
>
> I have a hard time trying to construct a double "for" loop and
create
> within the inner loop new objects (in this case vectors).
>
> I posted this question in a non-directly related with pure R-problems
> list (in grass-stats). In addition, I think I wasn't precise enough.
>
> Many thanks for your time in advance, Nikos
> ---
>
> The data
>
> * A list of 10 different names called "classifications"
> * "data.frames" starting with the "classification"
names and a common
> "_thresholds" suffix. Each "_thresholds" data.frame
contains 10 columns
> with numeric values (each column consists of 875 rows)
>
>
> The goal
>
> I want to create a new vector for each "classification" which
will hold
> 10 numeric values (e.g. some sum) one for each column in the
> "classification"-"thresholds" objects.
>
>
> 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],
> sum ( !is.na (
> get ( paste ( x, "_thresholds", sep = "" ) )[ ,i]
> )
> )
> )
> }
>
>
> The problem
>
> The above piece of code gives me 10 new vectors but with only 1 value
> (e.g. the last derived from the loop) _instead_ of 10.
>
>
> Questions
>
> 1. How can I construct this properly
> 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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
[[alternative HTML version deleted]]