我需要一些帮助。这是我的数据框:
> df1
g_1 g_2 g_3 g_4 g_5
species_1 canidae 0 0 0 0
species_2 canidae 0 equidae3 canidae 0
species_3 0 0 0 canidae 0
species_4 0 leptodactylidae equidae3 0 0
species_5 0 0 0 0 <NA>
structure(list(g_1 = c("canidae", "canidae", "0", "0", "0"),
g_2 = c("0", "0", "0", "leptodactylidae", "0"), g_3 = c("0",
"equidae3", "0", "equidae3", "0"), g_4 = c("0", "canidae",
"canidae", "0", "0"), g_5 = c("0", "0", "0", "0", "<NA>")), row.names = c("species_1",
"species_2", "species_3", "species_4", "species_5"), class = "data.frame")
并且想法是将每个项目值(例如canidae leptodactylidae
等替换为一个特定的数字。
在实际数据中,以ae结尾的项目数非常多。
最后,如果我们这样说:
canidae = 1
leptodactylidae = 2
equidae3=3
我们应该得到:
> df2
g_1 g_2 g_3 g_4 g_5
species_1 1 0 0 0 0
species_2 1 0 3 1 0
species_3 0 0 0 1 0
species_4 0 2 3 0 0
species_5 0 0 0 0 <NA>
如果有人在R中有一个科来做呢?谢谢您的宝贵时间。