Reshaping: Wide to Long

  • Wide means:
    • multiple measurement columns across each row
    • Each row includes a column for
      • revenue
      • profit
      • profit margin
  • Ling means:
    • only one measurement per row
    • likely multiple categories
    • The column financialCategory
      • tells me what type of measurement each value is
  • melt(){reshape2}
install.packages("reshape2")
library(reshape2)
  • format to longData:
longData <- melt(your original data frame, a vector of your category variables)
  • wide-to-long code

    • code assumes that all the other columns except fy and company are measurements -- items you might want to plot.
      companiesLong <- melt(companiesData, c("fy", "company"))
      
  • lists all the column in the data frame

    • assigning them to either id.vars or measure.vars,
    • changes the new column names from the default "variable" and "value":
companiesLong <- melt(companiesData, id.vars=c("fy", "company"),   measure.vars=c("revenue", "profit", "margin"), variable.name="financialCategory", value.name="amount")

Reference:

results matching ""

    No results matching ""