我想从我的数据集(BirdCount_2008_2018)"月"创建一个名为season的新变量。变量。月份出现在整数:1-12。我想安排3-5岁为春季,7-11岁为秋季,12-2为冬季,其他为夏季。我尝试输入
BirdCount_2008_2018$Season<-with(BirdCount_2008_2018,ifelse((Month==3|Month==4|Month==5),"Spring"),ifelse((Month==7|Month==8|Month==9|Month==10|Month==11),"Autumn"), ifelse((Month==12|Month==1|Month==2),"Winter","Summer"))
但是有一个错误,我一直在尝试类似的代码,但我仍然无法弄清楚发生了什么
有人能帮助我吗?我是R的新手,真的需要帮助!!!!
编辑:我已经尝试输入其他类似帖子的建议答案的代码,但我仍然无法创建新变量,请帮忙,我很失落,非常感谢。编辑:这是我输入的代码
BirdCount_2008_2018 %>% mutate(Season = ifelse((Month == 7 | Month == 8 | Month == 9 | Month == 10 | Month == 11), "Autumn", ifelse((Month == 12 | Month == 1 | Month == 2), "Winter", ifelse((Month == 3 | Month == 4 | Month == 5), "Spring","Summer"))))
结果:
# A tibble: 3,991 x 8
Species Sciname Year Month Day Count Location Season
Swan Goose Anser cygnoides 2008 1 NA 1 <NA> Winter
# ... with 3,981 more rows
但是当我查看表格时,季节的最后一栏没有出现。 First column of table view