zackymo21 - 11 months ago 47

R Question

I am currently having a problem utilizing R to compare each column within a specific matrix. I have attempted to compare each of the entire columns at once, and generate a true and false output via the table command, and then convert the number of trues that can be found to a numeric value and input such values in their respective places within the incidence matrix.

`For example, I have data in this type of format:`

//Example state matrix - I am attempting to compare c1 with c2, then c1 with c3, then c1 with c4 and so on and so forth

c1 c2 c3 c4

r1 2 6 3 2

r2 1 1 6 5

r3 3 1 3 6

And I am trying to instead put it into this format

//Example incidence matrix - Which is how many times c1 equaled c2 in the above matrix

c1 c2 c3 c4

c1 3 1 1 1

c2 1 3 0 0

c3 1 0 3 0

c4 1 0 0 3

Warning message:

In IncidenceMat[rat][r] = IncidenceMat[rat][r] + as.numeric(instances) :number of items to replace is not a multiple of replacement length

`rawData = read.table("5-14-2014streamW636PPstate.txt")`

colnames = names(rawData) #the column names in R

df <- data.frame(rawData)

rats = ncol(rawData)

instances = nrow(rawData)

IncidenceMat = matrix(rep(0, rats), nrow = rats, ncol = rats)

for(rat in rats)

for(r in rats)

if(rat == r){rawData[instance][rat] == rawData[instance][r] something like this would work in C++ if I attempted,

IncidenceMat[rat][r] = IncidenceMat[rat][r] + as.numeric(instances)

} else{

count = df[colnames[rat]] == df[colnames[r]]

c = table(count)

TotTrue = as.numeric(c[2][1])

IncidenceMat[rat][r] = IncidenceMat[rat][r] + TotTrue #count would go here #this should work like a charm as well

}

Any help would be greatly appreciated; I have also looked at some of these resources, however, I am still stumped

I tried this and this along with some other resources I recently closed.

Answer Source

How about this (note the incidence matrix is symmetric)?

```
df
c1 c2 c3 c4
r1 2 6 3 2
r2 1 1 6 5
r3 3 1 3 6
incidence <- matrix(rep(0, ncol(df)*ncol(df)), nrow=ncol(df))
diag(incidence) <- nrow(df)
for (i in 1:(ncol(df)-1)) {
for (j in (i+1):ncol(df)) {
incidence[i,j] = incidence[j,i] = sum(df[,i] == df[,j])
}
}
incidence
[,1] [,2] [,3] [,4]
[1,] 3 1 1 1
[2,] 1 3 0 0
[3,] 1 0 3 0
[4,] 1 0 0 3
```