基于R中的先前数字列将新的字 符串列添加到数据框

问题描述 投票:0回答:1

我有一个数据框,其中包含6种不同树种的40万棵树的数据。为每个物种分配了一个数字物种代码,该代码与特定物种相对应。我想添加另一列,列出每棵树的科学名称。物种代码不是连续的,因为该数据是根据丰度从163种490,000棵树中筛选出来的。这是类似于我所拥有数据的示例:

Index    Age    Species_code
0        45     14
1        47     32
2        14     62
3        78     126
4        40     14
5        38     17 
6        28     47

这是我想要获得的示例:

Index    Age    Species_code    Species
0        45     14              Licania_heteromorpha
1        47     32              Pouteria_reticulata
2        14     62              Chrysophyllum_cuneifolium
3        78     126             Eperua_falcata
4        40     14              Licania_heteromorpha
5        38     17              Simaba_cedron
6        28     47              Sterculia_pruriens

我一直在尝试]

if (Species_code == 14)
{
}

但是,这在输出中给了我TRUEFALSE

r string dataframe plyr data-manipulation
1个回答
0
投票

您可能想使用ifelse()功能。

但是由于您的代码清单很长,您可能还需要使用:

my_names <- numeric()
names[47] <- "Licania_heteromorpha"
names[63] <- "Chrysophyllum_cuneifolium"
...
df$Species <- names[df$Species_code]
© www.soinside.com 2019 - 2024. All rights reserved.