Skip to content

R for loop nested?

3 messages · Delia Shelton, R. Michael Weylandt, Kenn Konstabel

#
In what way doesn't this work?

You declare xx with one element and it prints.

Then on the next loop, you add another element, and it prints them both.

Then on the next loop, you add another element, and it prints all three.

And so on...

If you look at the output, you can see that it's a growing vector.
Perhaps you just mean to put print(xx) outside the loop to only get
the single final answer.

Michael

PS -- Those are multiple matrices, not just a single "crazy matrix."
On Tue, Oct 25, 2011 at 8:19 AM, Delia Shelton <delsshel at indiana.edu> wrote:
#
Hi there,

I'm adding a very small bit of something utterly useless to Michael's
response. When you start wondering why your code doesn't work the way
you expect, making it as simple as possible might be the first step.
For example, the following piece of code ...

TRUE

... is simpler than ...

!!!!TRUE

... and this, in turn, is simpler than ...

!(!(!(!(TRUE))))

... but they all result in  the same value.

A couple of ideas how to make your code easier to read:

if (((length(name$Behavior))) %% 2 != 0) { something }

Here you have an extra pair of parentheses that could just as well be deleted:

if ((length(name$Behavior)) %% 2 != 0) { something }

But length() of a variable in a data frame is just nrow() of that data
frame so you could simplify this further to ...

if (nrow(name) %% 2 != 0) { something }


And this one ...

                if (y %% 2 == 0) {next} else
                {q<-c(q, (c(name$Time[y]:name$Time[y +1])))}

... is particularly tough considering that you might just as well write

               if (y %% 2 != 0) q<-c(q, name$Time[y]:name$Time[y +1])

... or, if you like them a lot, ...
                if ((((y))) %% (((2))) == (((0)))) {next} else
                {q<-c((((q))), (c((((name$Time[y]))):(((name$Time[y
+(((1)))]))))))}

... would also do with some extra aesthetic pleasure.
I don't get it. The result of read.table is a data frame, so you may
use names instead of colnames, and the 3rd and 4th lines seem to be
unnecessary. If you just want to switch the first and the second
variable, then X[,2:1] would do it. So instead of that you could
write:

X <- read.table(file.choose(),header=FALSE)[,2:1]  # the name "name"
sounds confusing
names(X) <- c("Behavior", "Time")
X <- subset(X, Time < 3600)

... (the following single line would also do but is uglier)

X <- subset(structure(read.table(file.choose(), header=FALSE)[,2:1],
.Names=c("Behavior", "Time")), Time < 3600)

And it may help giving your objects some meaningful names. For example,

a <- list.files()

is fine but alternative (and some might say, better understandable at
first look) names to `a` could be, e.g., `files` or  `MYFILES` or
`list.of.files`.  And ....

        name<- read.table(i,header=F)

... again, you may name anything in whatever way but to call a data
table a `name` may be confusing to some readers. And saying that

xx<- c()

... is just another way of saying that xx <-  NULL . NULL is clearer
than c() as it makes your intention (to make an empty object and then
start adding something to it) more obvious. Besides, NULL is quicker
and more efficient.

Sorry for not giving any useful advice, it's late here.

Best regards,

Kenn Konstabel
On Tue, Oct 25, 2011 at 3:19 PM, Delia Shelton <delsshel at indiana.edu> wrote: