最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - non-numeric argument to binary operator when dataframe is all numbers - Stack Overflow

programmeradmin1浏览0评论

I have a dataframe of relative abundances of OTUs which looks like this (each row is a sample):

 df <- data.frame(OTU1 = proportions(sample(100, size=10, replace = TRUE)),
           OTU2 = proportions(sample(100, size = 10, replace = TRUE)),
           OTU3 = proportions(sample(100, size = 10, replace = TRUE)),
           OTU4 = proportions(sample(100, size = 10, replace = TRUE)),
           OTU5 = proportions(sample(100, size = 10, replace = TRUE)),
           OTU6 = proportions(sample(100, size = 10, replace = TRUE)),
           OTU7 = proportions(sample(100, size = 10, replace = TRUE)),
           OTU8 = proportions(sample(100, size = 10, replace = TRUE)),
           OTU9 = proportions(sample(100, size = 10, replace = TRUE)),
           OTU10 = proportions(sample(100, size = 10, replace = TRUE)))

I am using this code which has worked for me before to try and make a dataframe with diveristy indices using some base R and some functions from vegan:

 diversity = data.frame(Richness = apply(df>0,1,sum),
                        Shannon = diversity(df), 
                        Simpson = diversity(df, "simpson"))%>%
                        mutate(Sample = as.character(rownames(.)))

But I get an error message saying

   Error in as.vector(otu)%%1 : non-numeric argument to binary operator

My googling has told me maybe apply is not working because my columns are not numeric, but I have checked and they all are. I've checked each column using str(df) and all are numeric:

 str(df) 

'data.frame':   10 obs. of  10 variables:
$ OTU1 : num  0.047 0.132 0.15 0.167 0.122 ...
$ OTU2 : num  0.0843 0.0655 0.0524 0.0337 0.1086 ...
$ OTU3 : num  0.0684 0.1777 0.0215 0.084 0.0625 ...
$ OTU4 : num  0.1967 0.0328 0.1569 0.1429 0.1546 ...
$ OTU5 : num  0.1277 0.1295 0.0935 0.1385 0.0306 ...
$ OTU6 : num  0.1555 0.19856 0.17703 0.01914 0.00957 ...
$ OTU7 : num  0.01475 0.27434 0.00295 0.11209 0.13274 ...
$ OTU8 : num  0.0174 0.1766 0.2214 0.0398 0.0498 ...
$ OTU9 : num  0.1405 0.061 0.1608 0.0665 0.1017 ...
$ OTU10: num  0.0413 0.037 0.0152 0.037 0.2087 ...

But when I use is.numeric I get "false" which makes no sense to me!

is.numeric(df)
[1] FALSE

I have been driving myself crazy with this! Any help would be very much appreciated. Thanks in advance.

发布评论

评论列表(0)

  1. 暂无评论