[GRASS-stats] Create new vectors within a (double) loop in R
Nikos Alexandris
nikos.alexandris at felis.uni-freiburg.de
Fri May 22 08:06:25 EDT 2009
Hi list.
(Apologies in advance since this is actually a non-spatial R-related
question.)
I use the following one-liner to create a new vector made out of 10
values. Each value is _some_ sum of a column from a "Source" data frame.
# code:
for (i in 1:10) print ( sum ( !is.na (Source[ ,i]) ) ) -> TargetV[i]
I want to construct a loop over a set of "Source" data frames ( say they
are 10 ) and create, in total, 10 new vectors at once. All new vectors
will be made out of 10 values as in the example above.
Finally, I would like to have all this in a custom function (once it
works line-per-line).
I am stuck on the following "code" which gives me 10 new vectors but
each of them has only 1 numeric value instead of the desired 10.
# the data are:
# "classifications" is _just_ a list with names
# 10 vectors with a "_thresholds" suffix contain 10 columns with values
I want to use
# I want to create 10 vectors named with a "sums" suffix.
# the code:
# loop over Source data frames
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]
)
)
)
}
Question:
What is the correct way to create new variable(s) within a (double)
loop? The command "paste()" prints its results within quotes which, I
think, is a problem here??
Thanks, Nikos
More information about the grass-stats
mailing list