Error in code from split-apply-combine paper - how to resolve? - r

To try and get to grips with data manipulations in R, I've started reading Hadley's paper on split-apply-combine.
I'm on page 3 and trying to go through the code to understand it. Unfortunately the code is erroring and my reproduction is faithful (I've done c&p and handtyped). As I'm trying to learn this stuff and I'm right at the beginning I can't actually tell what's wrong with it. I tried it on both R2.5 and R3.0
library("MASS")
library("plyr")
data(ozone)
one<-ozone[1,1,]
month<-ordered(rep(1:12,length=72))
model<-rlm(one ~ month - 1)
deseas<-resid(model)
deseasf<-function(value) {rlm(value ~ month - 1)}
models<-aaply(ozone,1:2,deseasf)
deseas<-aaply(models,1:2,resid)
Where the models line errors with Error: Results must have one or more dimensions.
Can somebody tell me whether it works for them, or what needs to be fixed/amended if it doesn't and why?
PS - Can't check on http://plyr.had.co.nz/ for errata because my work proxy currently blocks the site!

It should be
models <- alply(ozone, 1:2, deseasf)
deseas <- ldply(models, resid)

It turns out this is a bug in aaply and Hadley has said he will look into it soon:
https://groups.google.com/forum/#!topic/manipulatr/kg2wDU96mGM

Related

truthTable in Rstudio Error: incorrect outcome specification

I was trying to do a QCA analysis of sufficeny in RStudio 4.1.3. using truthTable command. By running the code i get Error: wrong outcome specification. The outcome is written correctly. When I do an analysis of necessity with superSubset code everything works fine. Everything is coded numeric but it does not work with bivariate callibration either.
> ttCO21 <- truthTable( data = mydata, outcome = "CO21",
+ conditions = "GENDG1, FDI1, GDP1",
+ sort.by="incl, n", show.cases = TRUE, complete = TRUE)
**Error: Incorrect outcome specification.**
I had the same issue today and the solution was to delete any unnecessary columns (or create a new dataframe). I left only the calibrated conditions and outcome, because when I found this error, the raw data was in the same df, even if was not using it in the truthTable() command.
Because of the 3 hours i wasted on this problem i'll share my situation and solution. I had the exact same problem. creating a new df did not help, nor did removing unnecessary colums (wouldn't make sense anyway). Completely reinstalling en reloading the QCA package did work for me. Hope this helps someone.
you can try "comma" instead of "dot" (in “decimal") when you import file

R function seas() not found in v.4.1.0

I have a script that I have used several times over the past year. The last time I used it was 3 months ago. It was all working with no errors.
In the interim, I have upgraded R to v. 4.1.0. This may be a generic problem, but I've covered what I think are the obvious possible explanations (packages not installed, libs not loaded, typos, etc).
Here's the relevant code (all calls are successful up to the seas() call):
library(fpp2)
library(ggplot2)
library(seasonal)
library(seas) # Added recently in attempt to mitigate this issue
hh=6
ff=12
LT <- read.csv("C:\\...\\lt.csv")
LTts <- ts(LT, start=c(2007,1),frequency=12)
#...
LTx11 <- seas(LTts, x11="")
RESULT (Console output):
> LTx11 <- seas(LTts, x11="")
#Error in seas(LTts, x11 = "") : could not find function "seas"
Any tips or suggestions would be greatly appreciated.
After digging around a little more, I found the original text reference where I had learned to use the seas() function in Hyndman's text: https://otexts.com/fpp2/x11.html
In that text, I also found that seas() should be exposed by the seasonal library and verified this in the docs for that library.
After verifying this, added seasonal:: in front of the function and it worked. I wish I knew WHY this happened, but I don't. So here is the workaround.
Workaround:
LTx11 <- seasonal::seas(LTts, x11="")
If anyone can explain why this was necessary in v.4.1.0 and not earlier versions of R, please do!

why does ddply malfunction in this case?

In the book Machine learning for hackers Chapter4, there is a line of code contains ddply which is:
from.weight<-ddply(priority.train,.(From.EMail),summarize,Freq=length(Subject))
and it doesn't work.I have seen somewhere else saying that we can get the same result by changing the code to:
from.weight <- melt(with(priority.train, table(From.EMail)), value.name="Freq")
from.weight <- from.weight[with(from.weight, order(Freq)), ]
Is there any possibility that we still use ddply and make it?

Error when using expression as label in ggplot

I tried searching, but this bothers me quite a bit, as it in my opinion should be a simple line of coding. However, i keep getting an error when trying...
Firstly, this code works fine
scale_linetype_manual("",
values=c("pAl"=4,"pAlOH"=3, "pAl7OH17"=6, "pAl13OH34"=2,"pAlOH4"=1,"pAl2OH2"=5),
labels = c(expression("Al"^"3+"),
expression("Al(OH)"^"2+"),
expression("Al7(OH)"[17]^"4+"),
expression("Al13(OH)"[34]^"5+"),
expression("Al(OH)"[4]^"-"),
expression("Al2(OH)"[2]^"4+")))+
I do, however, want some of the numbers after Al to be subscripted too, but when trying
expression("Al"[2]"(OH)"^"2+") or expression("Al" ~ [2] ~ "(OH)" ~ ^ ~ "2+")
or whatever variant i can come up with, i keep getting an error.
Again, there's probably an easy fix. Sorry for my lack of experience in using R :)
library(grid)
e <- c("Al[2]~(OH)^'2+'",
"Al[7]~(OH)[17]^'4+'")
grid.newpage()
grid.text(parse(text=e), y=c(0.4,0.6))

R Language: Error in read.table(file.path(data.dir, file_name1)) : no lines available in input

I am having a hard time coding in R language. What I am trying to do is read large amount of data in to one data frame, and make pretty pictures.
This is what I have:
# assign data
file_name1<-"data1_txt"
file_name2<-"data2_txt"
data.dir<-"/...../Documents/R programing Language/"
for(i in 1:length(1)){
newData1<-read.table(file.path(data.dir, file_name1))
#Replace negative numbers with NA
xx <- which(datavalues<0)
datavalues[xx] <- NA
newData2<-read.table(file.path(data.dir,file_name2))
}
Error I have is:
Error in read.table(file.path(data.dir, file_name1)) :
no lines available in input
I am trying to figure out by myself, but I am very new to R language, and I don't have enough knowledge of functions in R. Please explain what this error means and advice on my coding.
Thank you very much,
Uka
Similar situation was solved here with similar question (I know this post is quite old). Recently I got such error parsing several files... The reason was some files were empty which makes sense of error message.
Anyway, just make sure your input will not be empty using try ou trycatch as suggested on mentioned link.

Resources