Reshape multiple value columns to wide format

前端 未结 5 520
难免孤独
难免孤独 2020-11-22 11:32

I have the following data frame and i want to use cast to create a \"pivot table\" with columns for two values (value and percent). Here is the data frame:

         


        
5条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 12:25

    data.table can cast on multiple value.var variables. This is quite direct (and efficient).

    Therefore:

    library(data.table) # v1.9.5+
    dcast(setDT(expensesByMonth), expense_type ~ month, value.var = c("value", "percent"))
    

提交回复
热议问题