dcast error: `Error in match(x, table, nomatch = 0L)`

Username picture Username · Sep 6, 2016 · Viewed 12.8k times · Source

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?

Answer

prateek1592 picture prateek1592 · Sep 6, 2016

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?