Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

r - cor.test ,"not enough finite observations"

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are not properly passing vector (i.e., dataframe column) into function. Consider passing string literals of data frame columns to be referenced with double brackets (and as.numeric() may not be necessary if columns are numeric types):

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)

Alternatively without data argument:

cor_result<-function(x, y){ 
   a<-cor.test(x=(x/y),y=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=cust_new$maintain_cust, y=cust_new$ttl_cust)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...