Simple filtering in R, but with more than one value -
i aware of how extract data based on condition, whenever try multiple conditions, struggle ensues. have data , want extract years df. here example df:
year value 2006 3 2007 4 2007 3 2008 5 2008 4 2008 4 2009 5 2009 9 2010 2 2010 8 2011 3 2011 8 2011 7 2012 3 2013 4 2012 6
now let's want 2008, 2009, 2010, , 2011. try
df<-df[df$year == c("2008", "2009", "2010", "2011"),]
doesn't work, then:
df<-df[df$year == "2008" & df$year == "2009" & df$year == "2010" & df$year == "2011",]
no error messages, empty df. missing?
you need use %in%
, not==
df[df$year %in% c(2008, 2009, 2010, 2011),] year value 4 2008 5 5 2008 4 6 2008 4 7 2009 5 8 2009 9 9 2010 2 10 2010 8 11 2011 3 12 2011 8 13 2011 7
Comments
Post a Comment