[R] Trouble with syntax

Uwe Ligges ligges at statistik.uni-dortmund.de
Sun Dec 7 16:05:43 CET 2003


Window Glass wrote:


[Looks like there was no answer on R-help yet]

> Hello,
>
> axsize <- max(i)

Let's assume i is a numeric (integer) vector:


> allsize <- axsize * axsize
> x <- array(c(1:allsize), dim=c(axsize, axsize))

You are going to construct a matrix, maybe it's simple to say

x <- matrix(1:allsize, axsize)


> x[1:allsize] <- 0

So, you are setting the whole matrix to 0 now? Isn't it better to 
initialize with 0 at once? E.g.:

x <- matrix(0, axsize, axsize)


> x[i] <- 1

Are you sure you want those 1s only in the first column?
You are indexing x still as a vector, not as a matrix!



> for (c in 1:axsize)

It's not a good idea to use a variable called "c" - there is a function 
c() (works here, though).


> {
>         ourgraph <- data.frame(edges=x,
> vertices=c(1:axsize), ith=c,
>                         components=list(1),
> communities=list(1), row.names=NULL)
>         browser()
> 
> When I get to browser, printing ourgraph$edges gives
> NULL while printing x gives the matrix with the
> correct values.
> What am I writing wrong?

A data.frame must contain an equal number of observations for each 
"column", so data.frame constructs the data.frame so that it fits. You 
are going to use a list!

  names(ourgraph)
or
  str(ourgraph)
will result in some deeper insights.

It doesn't make sense to overwrite ourgraph axsize times, BTW.

Uwe Ligges

> Regards,
> Wind0w Glass




More information about the R-help mailing list