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.
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
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