Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 463
Username Avatar asked Sep 06 '16 01:09

Username


1 Answers

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?

like image 100
prateek1592 Avatar answered Sep 24 '22 16:09

prateek1592