Hi,
It is probably more confusing with several steps combined, but you are
correct that it is because there are NAs. It is fairly common for R
functions to return NA if there are any NA values unless you
explicitly set an argument on what to do with missing values. A quick
look at ?cor clearly shows that there is such an argument with several
options. Try adding ', use = "pairwise.complete.obs" ' to
your
sapply call.
Hope that helps,
Josh
On Sun, Oct 9, 2011 at 12:47 AM, William Claster <dmfall2010 at yahoo.com>
wrote:> Hello. I am wondering why I am getting NA for all
in?cors=sapply(pred,cor,y=resp). I suppose that each column in pred has NAs in
them. Is there some way to fix this? Thanks
>
>
>> str(pred)
> 'data.frame': ? 200 obs. of ?13 variables:
> ?$ mnO2: num ?9.8 8 11.4 4.8 9 13.1 10.3 10.6 3.4 9.9 ...
> ?$ Cl ?: num ?60.8 57.8 40 77.4 55.4 ...
> ?$ NO3 : num ?6.24 1.29 5.33 2.3 10.42 ...
> ?$ NH4 : num ?578 370 346.7 98.2 233.7 ...
> ?$ oPO4: num ?105 428.8 125.7 61.2 58.2 ...
> ?$ PO4 : num ?170 558.8 187.1 138.7 97.6 ...
> ?$ Chla: num ?50 1.3 15.6 1.4 10.5 ...
> ?$ a1 ?: num ?0 1.4 3.3 3.1 9.2 15.1 2.4 18.2 25.4 17 ...
> ?$ a2 ?: num ?0 7.6 53.6 41 2.9 14.6 1.2 1.6 5.4 0 ...
> ?$ a3 ?: num ?0 4.8 1.9 18.9 7.5 1.4 3.2 0 2.5 0 ...
> ?$ a4 ?: num ?0 1.9 0 0 0 0 3.9 0 0 2.9 ...
> ?$ a5 ?: num ?34.2 6.7 0 1.4 7.5 22.5 5.8 5.5 0 0 ...
> ?$ a6 ?: num ?8.3 0 0 0 4.1 12.6 6.8 8.7 0 0 ...
>> str(y=resp)
> Error in length(object) : 'object' is missing
>> str(resp)
> ?num [1:200] 8 8.35 8.1 8.07 8.06 8.25 8.15 8.05 8.7 7.93 ...
>> cors=sapply(pred,cor,y=resp)
>> cors
> mnO2 ? Cl ?NO3 ?NH4 oPO4 ?PO4 Chla ? a1 ? a2 ? a3 ? a4 ? a5 ? a6
> ? NA ? NA ? NA ? NA ? NA ? NA ? NA ? NA ? NA ? NA ? NA ? NA ? NA
>>
> ? ? ? ?[[alternative HTML version deleted]]
In the future, please post in plain text, not HTML (as the posting
guide requests).
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/