0

I would like to create a new variable called season from my dataset(BirdCount_2008_2018) "Month" variable. The months appeared in integer: 1-12. I want to arrange 3-5 as spring, 7-11 as autumn, 12-2 as winter and the others as summer. I tried inputting

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"))

But there is an error, I kept trying similar codes, but I still can't figure out what is happening

Can someone help me? I am very new to R and really need help!!!!

EDIT: I have tried inputting codes as of the suggested answers to the other similar post, but I am still unable to create the new variable, please help, I am very lost, thanks a lot.

EDIT: Here is a code that I input

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"))))

And the result:

# 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

But when I view the table, the last column of Season did not show up. First column of table view

  • I have tried following the previous answers, but the error of `could not find function ":="` keep showing up, I am not sure what happened, please help – Sheena Chung May 31 '18 at 14:38
  • `BirdCount_2008_2018[,Season:=seasons["Month"]] Error in := (Season, seasons["Month"]) : could not find function ":="` Here is the code that I input according to the links given and the error that showed up – Sheena Chung May 31 '18 at 14:39
  • 1
    The function `:=` is from package `data.table`. Make sure you have it loaded – Sotos May 31 '18 at 14:51
  • @SheenaChung: if you prefer a solution using `ifelse` https://stackoverflow.com/questions/49702002/creating-season-variable-by-month-with-dplyr-in-r – Tung May 31 '18 at 15:06
  • @Tung , I have followed your answer to the question of the latest link you sent me, when I input `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 popped up and the season column is successfully built up, but when I `view(BirdCount_2008_2018)`, the Season column is indicated as NA, what is wrong with my input? Thanks for your answer – Sheena Chung May 31 '18 at 15:34
  • @SheenaChung: can you run `dput()` then add it to your question? Add all code and error there as well because comments don't work well with code. See more here [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Tung May 31 '18 at 15:37
  • @Tung, I am sorry that I do not quite understand what you mean, but I tried to make it look better and understandable by editing in my main post, is it clearer to you what I meant previously? I am so sorry for that because I am a total beginner in R and in StackOverflow – Sheena Chung May 31 '18 at 15:55
  • @SheenaChung: you need to share your data to make the problem reproducible. Every time you ask a question, always include the output of `dput` e.g. `dput(BirdCount_2008_2018)` and relevant code/error – Tung May 31 '18 at 16:03

0 Answers0