0

So I've been working off this R script for the last year, but I didn't write it and I need to add an extra group to a graph. Before I added the L1 Spanish group this plot printed beautifully [see the Old graph]. But now I just get the error of

"Error in barplot.default(coh, ylim = c(0, 100), ylab = c("% Source"), : 'height' must be a vector or a matrix"

A sample of the data:

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

The only changes I made to the following code from the original is adding L1 Spanish and text(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")

Any idea what's going on???

fuskerqq
  • 27
  • 8
  • Please show data, *coh*, on this data-secific question. Either `dput` enough rows to reproduce error or build random data structure equivalent to your actual. See [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Parfait Apr 26 '18 at 11:50
  • I couldn't get `dput` to work since there is a lot of data. I hope these additions help! – fuskerqq Apr 26 '18 at 12:31
  • Your posted code needed some slight fixing which I set up here on [rextester using posted data](http://rextester.com/XNHK51393). I receive no error and all four bars show: *English, L1 Spanish, L2 Spanish*, and *Japanese*. Do note: graph cuts off on rextester's Run It (F8) but if you copy code in an empty R environment, zoomed in graph renders fine. – Parfait Apr 26 '18 at 15:19
  • Specifically, your posted data does not have an *ID* field or *numSubjs* function. Always make sure code is runnable (i.e., reproducible) from our blank environments. – Parfait Apr 26 '18 at 15:23
  • Oh wow, I missed that numSubjs was even used in there.. Unfortunately, I can't upload that much of the data because of confidentiality reasons, but because I can't I'm continuing to get that error D: Thank you for your help, I guess I'll keep using brute force until it works. – fuskerqq Apr 27 '18 at 09:38
  • It was a factor issue... How did I not notice that. – fuskerqq Apr 27 '18 at 10:57

0 Answers0