Watts-College / crj-507-spring-2024

https://watts-college.github.io/crj-507-spring-2024/
MIT License
1 stars 0 forks source link

Lab05 Part#2 error in finding density #26

Closed Reb75 closed 6 months ago

Reb75 commented 6 months ago

When I put in the code to find the number of actors and events (N and M) it gives me a Null in the environment. When I attempt to find density, it gives me the error code "Error in L/(N*M): non-numeric argument to binary operator."


# load the libraries we need
> library( sna )
> library( network )
> 
> loc <- "https://github.com/jacobtnyoung/sna-textbook/raw/main/data/data-paul-revere-net.rds"
> PaulRevereNet <- readRDS( url( loc ) )
> 
> PaulRevereNet

>gplot(PaulRevereNet, gmode="twomode", usearrows=FALSE, vertex.cex=2, label.cex=1.2, main="Paul Revere Net")
> 
> L <- sum(PaulRevereNet)
> N <- dim(PaulRevereNet)[1]
> M <- dim(PaulRevereNet)[2]
> 
> density.PaulRevereNet <- L/(N*M)
Error in L/(N * M) : non-numeric argument to binary operator
CGUEVARR commented 6 months ago

looking at closed issues.... try adding the following to your initial setup...

dim( as.matrix( PaulRevereNet ) )[1]

Reb75 commented 6 months ago

I did that, which gave me 254. However, when I add the rest of the code to get L,M, and N, and then attempt to get my density, I am still getting the same error code.

jacobtnyoung commented 6 months ago

Hi @Reb75! Thanks @CGUEVARR for helping. @Reb75, you need to add the code that @CGUEVARR suggested to the rest of your script. The issue is that you need to have an object of class matrix for the dim() and sum() functions. You can do that two ways.

  1. Convert the object to class matrix like this: PaulRevereNet <- as.matrix( PaulRevereNet ) and then use the PaulRevereNet object in the functions.
  2. Use the as.matrix() function as suggested by @CGUEVARR to coerce the object.
Reb75 commented 6 months ago

Got it to work now, thank you for your help!

jacobtnyoung commented 6 months ago

@Reb75 awesome! And thanks @CGUEVARR again for helping!