I have a dataframe which has unique ids and other columns like this:
df = data.frame(id = c('id1', 'id2', 'id3'),
col_1 = c(0,1,0),
col_2 = c(1,0,0),
col_3 = c(1,1,NA),
col_4 = c(1,NA,NA))
How is it possible to melt the dataframe aiming to have 2 columns one with the id which will be repeated based on the number of columns and the second column will contain the value from the other columns.
Example of expected output:
df = data.frame(id = c('id1', 'id1', 'id1', 'id1', 'id2', 'id2', 'id2', 'id2', 'id3', 'id3', 'id3', 'id3'),
col_1 = c(0,1,1,1,1,0,1,NA,0,0,NA,NA))