【发布时间】:2021-09-29 10:34:49
【问题描述】:
我对 R 比较陌生,我正在尝试向数据框(称为 dGroup)添加一列,这取决于数据框中现有列(物种)中指定的名称。 在 dGroup 列中指定了属于三个组中的每个组的许多物种。这些是“Mus (Mouse)”或“Cerc (Possum)”应该在 dGroup 列中生成一个“SM”,“Tri (Brush tail)”或“Thyl (Wallaby)”或“Mac (Bennetts)”应该生成一个dGroup 列中的“MM”和“Mac (Bennetts)”应在 dGroup 列中生成“LM”。 数据框如下所示:
|Record|Species| dGroup|
|------|--------|-------|
|1| Mus (Mouse)|
|2| Cerc (Possum)|
|3| Tri (Brush tail)
|4| Thyl (Wallaby)
|5| Vom (Wombat)
|6| Mac (Bennetts)
我使用的代码如下: #import csv 确保字符串保留
df <- read.csv(file = 'SH.csv', stringsAsFactors=FALSE)
#add a new column to the dataframe called dGroup
df['dGroup']<-NA
#test with a single species note: this code words
df$dGroup = ifelse(df$Species == "Mus (Mouse)","SM", "NA")
#test combine two species in an ‘or’ condition note: this code does not work
df$dGroup = ifelse(df$Species == "Mus (Mouse)"||df$Species=="Cerc ( Possum)"){"SM", “NA”)}
我收到以下错误: 错误:意外的“{”在 有人可以帮忙吗?
【问题讨论】:
标签: r dataframe if-statement conditional-statements