5

I have a dataframe called df that looks something like this...

"ID","ReleaseYear","CriticPlayerPrefer","n","CountCriticScores","CountUserScores"
"1",1994,"Both",1,5,283
"2",1994,"Critics",0,0,0
"3",1994,"Players",0,0,0
"4",1995,"Both",3,17,506
"5",1995,"Critics",0,0,0
"6",1995,"Players",0,0,0
"7",1996,"Both",18,163,3536
"8",1996,"Critics",2,18,97
"9",1996,"Players",3,20,79

I want to flip the data frame around so the columns are like this:

"ReleaseYear","Both","Critics","Players"

The values for columns Both',CriticsandPlayerswould be then` for each.

When I try running this...

require(dcast)
chartData.CriticPlayerPreferByYear <- dcast(
    data = df,
    formula = ReleaseYear ~ CriticPlayerPrefer,
    fill = 0,
    value.var = n
)

... I get this error:

Error in match(x, table, nomatch = 0L) : 
  'match' requires vector arguments

What is the problem here? How do I fix it?

Username
  • 3,463
  • 11
  • 68
  • 111
  • 4
    `value.var = "n"`, the column name needs to be quoted. – Psidom Sep 06 '16 at 01:26
  • dcast isn't a package, AFAIK; do you mean reshape2? An alternative: `library(dplyr) ; library(tidyr) ; df %>% select(ReleaseYear, CriticPlayerPrefer, n) %>% spread(CriticPlayerPrefer, n)` – alistaire Sep 06 '16 at 01:47
  • @Psidom if you want me to mark this correct, go ahead and post your answer – Username Sep 06 '16 at 02:55

1 Answers1

4

You seem to be missing quotation marks?

data <- read.table(text='"ID","ReleaseYear","CriticPlayerPrefer","n","CountCriticScores","CountUserScores"
"1",1994,"Both",1,5,283
"2",1994,"Critics",0,0,0
"3",1994,"Players",0,0,0
"4",1995,"Both",3,17,506
"5",1995,"Critics",0,0,0
"6",1995,"Players",0,0,0
"7",1996,"Both",18,163,3536
"8",1996,"Critics",2,18,97
"9",1996,"Players",3,20,79"',header=T,sep=",")

library(reshape2)
dcast(data, ReleaseYear ~ CriticPlayerPrefer, value.var="n")

# ReleaseYear Both Critics Players
#       1994    1       0       0
#       1995    3       0       0
#       1996   18       2       3

This is what I get. Is it the desired result?

prateek1592
  • 547
  • 5
  • 13