Hello,
I have this table df1
and from it I want to create df2
. The new columns for df2
should be created based on the values that are present in all columns which contain the names col
. Thereafter, each test column should compare against all the columns and if its name is present score it as a 1 and not a 0. I would like a base R approach or a non transpose approach.
df1 <-
data.frame(
stringsAsFactors = FALSE,
Col1 = c("Test1", "Test1", "Test2", "Test2", "Test7"),
Col2 = c(NA, "Test3", "Test1", "Test2", NA),
Col3 = c(NA, "Test2", NA, NA, NA)
)
df2 <-
data.frame(
stringsAsFactors = FALSE,
Col1 = c("Test1", "Test1", "Test2", "Test2", "Test7"),
Col2 = c(NA, "Test3", "Test1", "Test2", NA),
Col3 = c(NA, "Test2", NA, NA, NA),
Test1 = c(1L, 1L, 1L, 0L, 0L),
Test2 = c(0L, 1L, 1L, 1L, 0L),
Test3 = c(0L, 1L, 0L, 0L, 0L),
Test7 = c(0L, 0L, 0L, 0L, 1L)
)