Using diversity() on character values

Hello everyone,

I've come to you with a question, please bear with me as I am pretty new to R in general.
Here is the problem:
I need to make a diversity index from a data table in R, and for this I found I could use the package "vegan".
Here is what I did:

simpson_diversity <- diversity(comT, index = "simpson")
where comT is the data frame with the data
console answers:
Error in diversity(comT, index = "simpson") : input data must be numeric

Indeed, the data I am using contains observations of species written in the format "character"."character"
example: u.u j.k l.m

Is there a way I can use an other function to directly interpret the characters?
Otherwise, how can I encode the characters as numbers?
I have tried filtering the data frame by unique values with:
unique_values<-unique(comT)
which gave /
[[1]]
[1] "f.i"

[[2]]
[1] "w.l"

[[3]]
[1] "t.w"

[[4]]
[1] "q.x"

[[5]]
[1] "e.n"

yet I am not sure how to make these match to the original data frame.

Thank you for your help,

its expected that you would have counts/numbers of named species ? does your data contain no numbers at all ?

To give you details, this is part of an assignment I am doing for my biostats course at university.
The data given is a txt file which I have imported with the built-in "Import data" tool from Rstudio, and it does not contain any number.
I am not sure what you mean by "counts/numbers", but I must be able to count how many of each species there are in the data so I can make calculations, like diversity().
I do not have to give back the data as numbers as only the results of calculations like diversity() are required

So its a text file with repeated names like

Dog
Cat
Dog
Dog
Lion
Cat
Dog
?



fil <- tempfile(fileext = ".data")
cat(
"Dog
Cat
Dog
Dog
Lion
Cat
Dog
"
,file = fil)

# 'fil' coule be a file defined by a file() call rather than my tempfile() example ####
(raw_text <- readLines(fil)) 
library(tidyverse)
(summarised_df <- enframe(raw_text) |> 
  group_by(value) |> 
  count() |> 
  ungroup())

(the_numbers <- deframe(summarised_df))

diversity(the_numbers, index="simpson")
> raw_text
[1] "Dog"  "Cat"  "Dog"  "Dog"  "Lion" "Cat"  "Dog" 
> summarised_df
# A tibble: 3 x 2
  value     n
  <chr> <int>
1 Cat       2
2 Dog       4
3 Lion      1
> the_numbers
 Cat  Dog Lion 
   2    4    1 
> diversity(the_numbers, index="simpson")
[1] 0.5714286

Hey, thanks for the lenghty answer, I'll try that with the data I have.

So, I've tried your solution, but it doesn't seem to be able to ungroup the values. Let me show you, I did:

fil <- tempfile(fileext = "com11.txt")
cat("f.i w.l t.w q.x e.n r.p b.f.........b.f b.f q.d",file = fil)
(raw_text <- readLines(fil))
library(tidyverse)
(summarised_df <- enframe(raw_text) |> 
    group_by(value) |> 
    count() |> 
    ungroup())

(the_numbers <- deframe(summarised_df))
diversity(the_numbers, index="simpson")

Console gives at the end:
1

diversity(the_numbers, index="simpson")
[1] 0
View(summarised_df)
fil <- tempfile(fileext = "com11.txt")

I put as attached the screenshot of the environnment in order how do the objects look like, with underneath the view of summarised_df

Thanks again,

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.