White Big White Big - 1 month ago 22
R Question

matching and recording in R

I have a data frame like this, called

df
:

a b c d e f
b c f a a a
d f a b c c
f e d f f d


The first row is actually the column name. Let's take an example to explain the meaning here:
df[1,1]
is
b
, which means there is relation from
a
to
b
, so the values in the column means there is relation from the 'column name' to that entry.

I want create a
matrix(df1)
with 6*6 dimension, column and row names are both column names of
df
. The (i,j) entry is 1, if there is relation from 'i' to 'j', otherwise, 0.

The output I want is:

a b c d e f
a 0 1 0 1 0 1
b 0 0 1 0 1 1
c 1 0 0 1 0 1
d 1 1 0 0 0 1
e 1 0 1 0 0 1
f 1 0 1 1 0 0


How to do this with a loop in R?

How to do this without a loop, and only use basic R?

How to do this using some fancy packages in R?

Answer

Using the reshape2 package, this is one way to go. My sample data has all columns as character. You use melt() to reshape your data in a long format. Then, you use dcast() from the same package.

library(magrittr)
library(reshape2)

melt(mydf, measure.vars = names(mydf)) %>%
dcast(variable ~ value, length)

  variable a b c d e f
1        a 0 1 0 1 0 1
2        b 0 0 1 0 1 1
3        c 1 0 0 1 0 1
4        d 1 1 0 0 0 1
5        e 1 0 1 0 0 1
6        f 1 0 1 1 0 0

EDIT As mentioned below by akrun, you can do all work using recast() in the reshape2 package.

recast(mydf, measure.var= names(mydf),variable~value, length)

DATA

mydf <- structure(list(a = c("b", "d", "f"), b = c("c", "f", "e"), c = c("f", 
"a", "d"), d = c("a", "b", "f"), e = c("a", "c", "f"), f = c("a", 
"c", "d")), .Names = c("a", "b", "c", "d", "e", "f"), class = "data.frame", row.names = c(NA, 
-3L))