melt + strsplit, or opposite to aggregate

前端 未结 4 1840
忘了有多久
忘了有多久 2021-01-22 07:32

I have a little question that seems to be so easy in concept, but I cannot find the way to do it...

Say I have a data.frame df2 with a column listing car brands and anot

4条回答
  •  独厮守ぢ
    2021-01-22 08:18

    Playing around, I have found a way to do the trick, even though it may be quite dirty:

    df1 <- data.frame(model=as.character(melt(strsplit(df2$models,','))$value), brand=as.character(df2[match(melt(strsplit(df2$models,','))$L1, rownames(df2)),]$brand))
    

    It is not the best solution, since the data.frames actually have many more columns, and I would not want to go one by one... If someone knows a prettier way to solve this, I would appreciate it!

提交回复
热议问题