R - reshape dataframe from duplicated column names but unique values -


hi have dataframe looks following

enter image description here

i want apply function reshapes this

enter image description here

how that?

here 1 option work. w loop through unique names of dataset, create logical index ==, extract columns, unlist, create data.frame, , cbind or use data.frame (assumption number of duplicate elements equal each set)

 data.frame(lapply(unique(names(df1)), function(x)      setnames(data.frame(unlist(df1[names(df1)==x], use.names = false)), x))) #  type model make #1        b    c #2    d     e    f 

data

df1 <- data.frame(type = "a", model = "b", make = "c", type = "d",       model = "e",       make = "f", check.names=false, stringsasfactors=false) 

Comments