I am new to R and have been at this problem for hours. I can't seem to find the right answer online and therefore turn to you guys. Other posts that I have seen use 3 columns instead of two.
I have a df with two columns: (company) name and id. Example df:
Name id
1 NameA 111
2 NameB 222
3 NameC 111
4 NameD 444
5 NameA 555
I want to turn this column into a two-mode (incidence) matrix with the row names being the names of the companies and the column names being the id number. Example desired result:
111 222 333 444 555
1 NameA 1 0 0 0 1
2 NameB 0 1 0 0 0
3 NameC 1 0 0 0 0
4 NameD 0 0 0 1 0
I have tried multiple forms of as.matrix and data.matrix but it doesn't give me the right format. Perhaps I'm doing them wrong. Can someone help me?
(As a bonus: If possible, I want to turn this two mode matrix into a one mode matrix next. I will be eternally grateful if someone knows how to do this too.)
Data in dput
format
df1 <-
structure(list(Name = c("NameA", "NameB", "NameC",
"NameD", "NameA"), id = c(111L, 222L, 111L, 444L, 555L)),
class = "data.frame", row.names = c("1", "2", "3", "4", "5"))