-2
Part1 = subset(mvt, mvt$Year == 2007,2008,2009,2010,2011,2012)

Printing part1 giving me null ? can I know whats wrong with the line ... I am a beginner . tnx for the patience.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197

3 Answers3

2

You need to use the %in% operator.

x <- data.frame(letters = sample(letters),
                ints = 1:length(letters)
)

> subset(x, subset = x$letters %in% c("r", "o", "m", "a", "n"))
   letters ints
4        r    4
6        m    6
9        n    9
10       a   10
12       o   12
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
-2
> Part1 <- subset(mvt, mvt$Year %in% c(2007:2012))
> Part1
  Year Value
1 2007     1
2 2008     2
3 2009     3
4 2010     4
5 2011     5
6 2012     6
Sagar
  • 2,778
  • 1
  • 8
  • 16
  • @NegativeVoters - Why is this one being voted negative? It provides the desired output... – Sagar Aug 28 '17 at 14:06
  • Though it indeed solves the problem, there can be 2 reasons for downvote: this solution has already been posted prior to your post and you're not giving any explanation around your lines of code (not even the example data used) to help the OP. – Cath Aug 28 '17 at 14:16
  • It's just timing of posting the answers. I was editing the question as well (which is to be peer-reviewed). Doesn't make any sense to negative vote. Duplicate answers can be marked duplicate in comments. – Sagar Aug 28 '17 at 14:18
  • I actually voted to reject your edit, as you're not supposed to add example data and/or desired output in an edit. Instead, you can ask OP to provide some. I was just giving you possible reasons ;-) – Cath Aug 28 '17 at 14:21
  • Got it. I agree with what you are saying. I myself review and triage posts. – Sagar Aug 28 '17 at 14:24
  • :-) (btw, you don't mark answer as "duplicate", only questions. "Duplicate answers" can only lower the quality of a Q/A post) – Cath Aug 28 '17 at 14:26
  • I meant adding "Duplicate" in the comments. – Sagar Aug 28 '17 at 14:28
  • 1
    yes but still, it won't help – Cath Aug 28 '17 at 14:32
  • thnx it worked !!!! – Nischal G Aug 30 '17 at 06:06
-3

I would suggest using the %in% operator, but you can also use the or operator.

Year <- c(2008, 2008, 2009, 2010, 2012, 2012, 2015)
Letter <- sample(letters)[1:length(Year)]
mvt <- data.frame(Year, Letter)
> mvt
  Year Letter
1 2008      z
2 2008      a
3 2009      c
4 2010      l
5 2012      k
6 2012      w
7 2015      r

subset(mvt, mvt$Year == 2008 |  mvt$Year == 2012)
> subset(mvt, mvt$Year == 2008 |  mvt$Year == 2012)
  Year Letter
1 2008      z
2 2008      a
5 2012      k
6 2012      w
Katie
  • 362
  • 3
  • 14