如何将数据集Especies列物种名替换为1-8数值适配Moran's I分析
操作方案
你可以直接通过R代码完成列转换,转换后即可满足Moran's I分析的数值输入要求:
- 首先将你提供的数据集赋值到变量:
df <- 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))
- 选择对应方法转换Especies列:
- 方法一:按当前数据中物种的出现顺序,依次映射为1-8:
df$Especies <- as.integer(factor(df$Especies, levels = unique(df$Especies)))
- 方法二:如果需要严格匹配参考标注的编号对应关系,避免后续顺序变动导致编号错乱,可手动指定映射规则:
# 可根据你的实际编号要求调整下方对应值 species_map <- c( "Strix_varia" = 1, "Strix_rufipes" = 2, "Strix_occidentalis" = 3, "Strix_aluco" = 4, "Strix_uralensis" = 5, "Strix_woodfordii" = 6, "Strix_leptogrammica" = 7, "Strix_nebulosa" = 8 ) df$Especies <- species_map[df$Especies]
转换完成后,Especies列就会变为整数类型的1-8数值,可直接代入Moran's I分析流程使用。如果后续需要核对物种和编号的对应关系,保留species_map向量即可快速回溯。
内容的提问来源于stack exchange,提问作者Miguel Figueroa
相关产品推荐
相关产品推荐

