I use two different ways to use cor.test
, one in my own function, the other uses cor.test
directly. When I use cor.test
in my own function, it appeared an error, how could it happen?
This was OK
cor.test(x=cust_new$maintain_cust/cust_new$ttl_cust,
y=cust_new$ttl_cust,alternative="two.sided",
method="pearson",conf.level=0.95)
The following would give the error :
"not enough finite observations"
cor_result<-function(x,y,data){
a<-cor.test(x=as.numeric(data$x)/as.numeric(data$y),
y=as.numeric(data$y),
alternative="two.sided",method="spearman",
conf.level=0.95)
r<-a$estimate
p<-a$p.value
c<-data.frame(r=r,p=p)
return(c)
}
d<-cor_result(x=maintain_cust,y=ttl_cust,data=cust_new)
The following would give the error :
'y' must be a numeric vector"
cor_result<-function(x,y,data){
a<-cor.test(x=data$x/data$y,y=data$y,
alternative="two.sided",method="spearman",conf.level=0.95)
r<-a$estimate
p<-a$p.value
c<-data.frame(r=r,p=p)
return(c)
}
d<-cor_result(x=maintain_cust,y=ttl_cust,data=cust_new)
dput(cust_new),a few sample:
structure(list(data_month = structure(c(16953, 16983, 17014,
17045, 17075, 17106, 16953, 16983, 17014, 17045), class = "Date"),
ttl_cust = c(383L, 580L, 735L, 850L, 952L, 1062L, 2418L,
2492L, 2515L, 2550L), maintain_cust = c(179L, 266L, 355L,
413L, 448L, 508L, 935L, 1026L, 1091L, 1143L)), row.names = c(NA,
10L), class = "data.frame", .Names = c("data_month", "ttl_cust",
"maintain_cust"))
See Question&Answers more detail:
os