0

I´d like to change the first data column named "Especies" and the other species names below it; (i.e "Strix_varia, Strix_rufipes...) and make them become the numbers 1 to 8 enclosed in red from link.

https://pasteboard.co/cIsmnATW0wMl.png

I´m working with Moran´s I and having the column "Especies" as data throws me incorrect results. Any help will be great! Thanks!

Heres my dput():

structure(list(Especies = c("Strix_varia", "Strix_rufipes", "Strix_occidentalis", 
"Strix_aluco", "Strix_uralensis", "Strix_woodfordii", "Strix_leptogrammica", 
"Strix_nebulosa"), Notas.segundo = c(2.9, 4.3, 2.9, 1.3, 1, 3, 
3.1, 1.1), Notas.llamado = c(6.3, 13.5, 12.2, 5, 3, 6, 4, 9.3
), Duracion.llamado = c(2.9, 2.9, 5.3, 4, 4.5, 1.6, 1.5, 7.3), 
    Frecuencia.minima = c(149.4, 157.4, 167, 314.7, 75.3, 149.3, 
    212.2, 147.5), Frecuencia.maxima = c(518.6, 564.8, 594.3, 
    846.2, 394.9, 438.4, 396.8, 263.8), Ancho.banda = c(369.1, 
    407.3, 427.2, 531.5, 319.6, 289, 184.6, 116.3), Frecuencia.central = c(522.1, 
    551.8, 589.9, 844, 385.9, 429, 374.9, 255.2)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -8L))
  • Please, provide *minimal reproducible example*. This includes a sample dataset such as by using the `dput` command. See here for more information: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – Colombo Oct 26 '21 at 19:15
  • Thanks for the comment, now i´ve added it. – Miguel Figueroa Oct 26 '21 at 19:38
  • If your dataframe is called `df` you can do `df <- tibble::column_to_rownames(df, 'Especies')` – Ronak Shah Oct 27 '21 at 05:19

1 Answers1

0

Assuming that in your table, you have one row per species and species do not repeat, simple data$Especies = seq_along(data$Especies) will do the job. I would suggest keeping the original table so you remember what code belongs to the species, such as with data$id = seq_along(data$Especies).


data = structure(list(Especies = c("Strix_varia", "Strix_rufipes", "Strix_occidentalis", 
"Strix_aluco", "Strix_uralensis", "Strix_woodfordii", "Strix_leptogrammica", 
"Strix_nebulosa"), Notas.segundo = c(2.9, 4.3, 2.9, 1.3, 1, 3, 
3.1, 1.1), Notas.llamado = c(6.3, 13.5, 12.2, 5, 3, 6, 4, 9.3
), Duracion.llamado = c(2.9, 2.9, 5.3, 4, 4.5, 1.6, 1.5, 7.3), 
    Frecuencia.minima = c(149.4, 157.4, 167, 314.7, 75.3, 149.3, 
    212.2, 147.5), Frecuencia.maxima = c(518.6, 564.8, 594.3, 
    846.2, 394.9, 438.4, 396.8, 263.8), Ancho.banda = c(369.1, 
    407.3, 427.2, 531.5, 319.6, 289, 184.6, 116.3), Frecuencia.central = c(522.1, 
    551.8, 589.9, 844, 385.9, 429, 374.9, 255.2)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -8L))

# If you don't want to overwrite your data:
data$id = seq_along(data$Especies)
# If you are OK with overwriting your data
data$Especies = seq_along(data$Especies)

If the species names are not unique and can repeat, this means that two species will have a different id. If that is not what you want, you can use factor():

data$id = as.numeric(factor(data$Especies))

Alternatively, you can create an encoding of your own by creating a named vector and use it to translate species names to id:

names = unique(data$Especies)
coding = seq_along(names)
names(coding) = names

data$id = coding[data$Especies]
Colombo
  • 428
  • 3
  • 12
  • I tried to copy your dput, and still shows me The "Especies" column as data. Also i noticed using this symbol $ throws me an error. Im sorry for the inconvenience but I´ve just learned to use RStudio. – Miguel Figueroa Oct 26 '21 at 20:47
  • "I tried to copy your dput, and still shows me The "Especies" column as data." -- Did you read what I wrote or just blindly copy-pasted the code block? I have edited the code block so it can be blindly copy-pasted. "Also i noticed using this symbol $ throws me an error." -- You need to be more specific then that. – Colombo Oct 26 '21 at 21:01