简体   繁体   中英

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

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', Critics and Players would be the n` 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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM