-3

I have a large set of financial statements imported into R and need to summarize the account names (VarN in the example below) by the column Years (YN) below and by amount (vN) below. I have searched for solutions using reshape and dplyr, but to no avail. I have:

Var1 Y1 v1
Var2 Y1 v2
Var3 Y1 v3
Var1 Y2 v4
Var2 Y2 v5
Var3 Y2 v6

I need to convert to:

     Y1  Y2  
Var1 v1  v4
Var2 v2  v5 
Var3 v3  v6
Josh. D
  • 47
  • 6

1 Answers1

0

Using reshape library on a dataframe df : http://www.statmethods.net/management/reshape.html

library(reshape)

column1 <- c("Var1", "Var2", "Var3", "Var1", "Var2", "Var3")
column2 <- c("Y1", "Y1", "Y1", "Y2", "Y2", "Y2")
column3 <- c("v1", "v2", "v3", "v4", "v5", "v6")


df <- data.frame(column1, column2, column3)
x <- c("Var", "y", "v")
colnames(df) <- x

mdata <- melt(df, id=c("Var", "y"))
cast(mdata, Var ~ y, value = 'v')

Don't forget to melt the data first.

Vincent K
  • 568
  • 5
  • 9