R:'height'必须是向量或矩阵错误,大数据集借用脚本

时间:2018-04-26 11:32:19

标签: r bar-chart

所以我去年一直在处理这个R脚本,但我没有写它,我需要在图表中添加一个额外的组。在我添加L1 Spanish组之前,这个情节打印得很漂亮[见 Old graph]。但现在我得到

的错误
  

“barplot.default中的错误(coh,ylim = c(0,100),ylab = c(”%Source“),   :'height'必须是向量或矩阵“

数据样本:

L1          SCond   isSource    aspectNames
L1 Spanish  I       1           Imperfective
L1 Spanish  I       1           Imperfective
L1 Spanish  P       0           Perfective
L1 Spanish  P       0           Perfective
L1 Spanish  I       0           Imperfective
L1 Spanish  I       0           Imperfective
English     P       1           Perfective
English     I       1           Imperfective
English     I       1           Imperfective
English     P       1           Perfective
English     P       1           Perfective
English     I       1           Imperfective
L2 Spanish  P       1           Perfective
L2 Spanish  P       0           Perfective
L2 Spanish  P       0           Perfective
L2 Spanish  I       0           Imperfective
L2 Spanish  I       1           Imperfective
L2 Spanish  P       1           Perfective
Japanese    I       1           Imperfective
Japanese    I       1           Imperfective
Japanese    P       1           Perfective
Japanese    P       1           Perfective
Japanese    P       1           Perfective
Japanese    P       1           Perfective

我对以下代码所做的唯一更改是添加L1 Spanishtext(2.3,-40, "Error bars +/- 1 SE")

dat <- subset(dat, SCRF != "A")

dat$isSource <- ifelse(dat$SCRF=="S", 1, 0) 


tapply(dat$isSource, list(dat$SCond, dat$L1), mean)
tapply(dat$isSource, list(dat$SCond), mean)
tapply(dat$isSource, list(dat$L1), mean)

coh <- rbind()
coh.ses <- rbind()
for(g in levels(dat$L1)){

    w <- subset(dat, L1==g)
    w.subj <- 100*tapply(w$isSource, list(w$SCond, w$ID), mean)

    mean.subj <- apply(w.subj, 1, mean, na.rm=TRUE)
    # calculate the standard deviation
    sd.subj <- apply(w.subj, 1, sd, na.rm=TRUE)
    # calculate the lengths (number subjects)
    ns.subj <- apply(w.subj, 1, numSubjs)
    # calculate the standard errors
    ses <- sd.subj/sqrt(ns.subj)

    print(mean.subj)

    coh <- cbind(coh,mean.subj)
    coh.ses <- cbind(coh.ses, ses)
}

par(mar = c(4, 4, 0, 2)+2)
bottom = paste("Language group")
CRnames = c("English", "L1 Spanish","L2 Spanish", "Japanese")
legendText = c("Imperfective", "Perfective")
xs <- barplot(coh, ylim=c(0,100), ylab=c("% Source"), names.arg=CRnames,  
              cex.names=1.7, cex.lab=2, sub=bottom, font=2, 
              font.sub=1,cex.sub=1.7,las=1, 
              legend=F,  beside=T)
legend(3.8, 100, text.width=2, legendText, fill=c("gray35", "gray90"), cex=1)
text(2.3,-40, "Error bars +/- 1 SE")

知道发生了什么???

0 个答案:

没有答案