[R] create vectors within a double loop
Nikos Alexandris
nikos.alexandris at felis.uni-freiburg.de
Sat May 23 16:32:52 CEST 2009
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?
More information about the R-help
mailing list