-2

The links provided do not solve the issue. Here is my edited problem:

There are hundreds of columns and I need to run cor for several.

1) I have to delete "character" columns from my df before running cor.

2) I also have to convert "logical" columns to "numeric". These "logical" columns are already in format of 1 or 0.

3) In each row are additional potential issues: (A) blank cells, (B) cells that say "N/A"

4) Basically, looking to run cor and trying to remove obstacles in way of doing that.

sapply(dfcor,class)
cor(dfcor, use = "complete.obs")

Error message:

Error in cor(dfcor, use = "complete.obs") : 'x' must be numeric

Gainz
  • 1,721
  • 9
  • 24
Ray
  • 25
  • 4
  • 1
    You can also try: `purrr::discard(dfcor,is.character)`. – NelsonGon Jun 26 '19 at 14:04
  • 1
    Ray, generally it is a good idea to make a simple example that illustrates the full problem you are facing; some guidance here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/28481250#28481250 Also, it is sometimes useful to isolate one problem at a time (eg, I don't think R has such a thing as blank values, so might want to fix that first?). – Frank Jun 26 '19 at 14:27
  • 2
    Your description of the problem is not adequate without code to reproduce the issue, since "blanks" is unclear; logical columns (in R lingo) cannot have "N/A" so that term is also ambiguous here; and so on. This is typical for R questions - it is necessary to provide code others can copy-paste to see the issue. As noted in the link, "This does take some work but seems like a fair trade-off since you are asking others to do work for you." `cor` runs just fine on what R calls logical columns, btw. Try `cor(data.frame(a = c(T,F,T), b = c(F,F,T)))` – Frank Jun 26 '19 at 14:49
  • 1
    @Frank though correlations on binary variables ? Probably not the best idea... – Cath Jun 26 '19 at 15:23

1 Answers1

1

We create a logical vector by checking whether the class is character, remove those columns by negating (!) and get the cor on those subset of columns

i1 <- sapply(dfcor, is.character)
cor(dfcor[!i1], use = "complete.obs")

The main issue regarding the post is why the error occurred. It is due to the fact that ?cor expects a numeric column/matrix/data.frame according to ?cor

x - a numeric vector, matrix or data frame.

akrun
  • 874,273
  • 37
  • 540
  • 662