r - new column created in dataframe based on conditions in other columns - but data being read incorrectly -
i have dataframe describing observations of bird species in various locations based on month , year. looks this:
common.name observation.count locality month year bushtit 1 vancouver jan 2000 lapland longspur 1 vancouver - general area jan 2000 mew gull 1 vancouver jan 2000 american coot 4 maplewood 00 jan 2000 american coot 2 maplewood 00 jan 2000 american coot 1 iona island (general) jan 2000
i trying create column in data frame called "season" groups months of jan, feb, mar , calls them winter , groups months oct, nov, dec fall. code wrote this:
metrobirds$season<-ifelse(metrobirds$month==c("jan","feb","mar"),"winter","fall")
however, when view dataframe, r has not correctly grouped data in new column. example, many of rows jan, feb, or mar indicated fall , correctly indicated winter. what's wrong code? suggestions correct error?
when read csv file r, converted columns identified factors characters (e.g. common name, month, , locality), columns should being read characters.
thanks this!
try using %in%
,
metrobirds$season<-ifelse(metrobirds$month %in% c("jan","feb","mar"),"winter","fall")
Comments
Post a Comment