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?
create vectors within a double loop
3 messages · jim holtman, Nikos Alexandris
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090523/2149764b/attachment-0001.pl>
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?