I want to create a matrix with 3 columns and many rows assigning 1 or 0 if the condition is satisfied.
I have data stored in 3 variables
df1 <- data.frame(names=c("A","B","C","D","E","F"))
df2 <- data.frame(names=c("A","B","C","F"))
df3 <- data.frame(names=c("E","F","H"))
output will be
df1 df2 df3
A 1 1 0
B 1 1 0
C 1 1 0
D 1 0 0
E 1 1 1
F 1 0 1
H 0 0 1
In first row if A is present in dataset then I will assign 1 under each column and 0 if A not present in dataset
Here is what I have tried
DF <- rbind(df1,df2,df3)
for (i in DF) {
for (j in 1:length(df1$names)) {
if(i == df1$names[j]){
A3 <-data.frame(paste0("",i),paste0(1),paste0(0),paste0(0))
names(A3) <- NULL
}
else{
A3 <-data.frame(paste0("",i),paste0(0),paste0(0),paste0(0))
}
}
}
I have written this code only for df1 but its very slow because I have more than 1500 rows in my orignal data set. What would be the fastest way to do it?