数据文件名:toys
假设我有以下数据框:
ID Name
1 Green Ball
2 Red Ball
3 Blue Bat
4 Green Bat
5 Blue Ball
6 Ball
7 Bat
我想通过在名称中搜索颜色来添加一个新变量"Color"。
ID Name Color
1 Green Ball Green
2 Red Ball Red
3 Blue Bat Blue
4 Green Bat Green
5 Blue Ball Blue
6 Ball Other
7 Bat Other
我从未使用过R语言,也不确定该如何开始。我尝试了以下内容但没有成功。
toys$Color <- (
if toys$Name = "Green", Color "Green"
else if toys$Name = "Red", Color "Red"
else if toys$Name = "Blue, Color "Blue"
else toys$Name = "Other"
)
我非常希望能够协助您解决这个问题。
谢谢。