R error “sum not meaningful for factors”

后端 未结 1 699
滥情空心
滥情空心 2020-11-27 07:24

I have a file called rRna_RDP_taxonomy_phylum with the following data :

364  \"Firmicutes\"            39.31
244  \"Proteobacteria\"        26.35
218  \"Act         


        
相关标签:
1条回答
  • 2020-11-27 07:56

    The error comes when you try to call sum(x) and x is a factor.

    What that means is that one of your columns, though they look like numbers are actually factors (what you are seeing is the text representation)

    simple fix, convert to numeric. However, it needs an intermeidate step of converting to character first. Use the following:

    family[, 1] <- as.numeric(as.character( family[, 1] ))
    family[, 3] <- as.numeric(as.character( family[, 3] ))
    

    For a detailed explanation of why the intermediate as.character step is needed, take a look at this question: How to convert a factor to integer\numeric without loss of information?

    0 讨论(0)
提交回复
热议问题