Question

I have a data set that looks like this:

   Group       Year                 Height
1     1.0      2004-2005               27
2     1.0      2005-2006               32600
3     2.0      2005-2006               520
4     1.0      2006-2007               216
5     2.0      2006-2007               39059
6     1.0      2006-2007               428
7     1.0      2007-2008               10624
8     2.0      2007-2008               30391
9     3.0      2007-2008               7450
10    4.0      2007-2008               234
11    1.0      2008-2009               2487
12    2.0      2008-2009               170
13    3.0      2008-2009               2606
14    4.0      2008-2009               519
15    5.0      2008-2009               54857
16    1.0      2009-2010               2272
17    2.0      2009-2010               3592
18    3.0      2009-2010               4792
19    4.0      2009-2010               75292
20    5.0      2009-2010               7555
21    6.0      2009-2010               9185
22    2.0      2010-2011               2073
23    3.0      2010-2011               582
24    4.0      2010-2011               6248
25    5.0      2010-2011               215
26    6.0      2010-2011               9153
27    7.0      2010-2011               3831
28    3.5           2011               5560
29    4.5           2011               1396

I have created an index to remove certain groups as well as year classes. For example

logical= (mydata$Group != 1 & mydata$Group != 2 & mydata$Year !=2011)
mydata_wk = mydata[logical,]

Now- I would like to plot this data. My problem is that when I plot this using the command below the X axis still shows the years that I deleted via indexing. For example, on the plot it will show 2011 which I deleted using the index above. I have tried to convert the same set to a data.matrix but it turns the years into numbers which correspond to the original data frame's year column which I DO NOT want to plot. What I want is to plot just the values that are in the final data frame after indexing. Any thoughts on this?

plot(mydata_wk$Group, mydata_wk$Year)

Thank you in advance.

Was it helpful?

Solution

is this the output you require

mydata<-read.table(text='  Group  ,Year,      Height
    1.0   ,2004-2005,       27
    1.0   ,2005-2006,       32600
    2.0   ,2005-2006,       520
    1.0   ,2006-2007,       216
    2.0   ,2006-2007,       39059
    1.0   ,2006-2007,       428
    1.0   ,2007-2008,       10624
    2.0   ,2007-2008,       30391
    3.0   ,2007-2008,       7450
    4.0   ,2007-2008,       234
    1.0   ,2008-2009,       2487
    2.0   ,2008-2009,       170
    3.0   ,2008-2009,       2606
    4.0   ,2008-2009,       519
    5.0   ,2008-2009,       54857
    1.0   ,2009-2010,       2272
    2.0   ,2009-2010,       3592
    3.0   ,2009-2010,       4792
    4.0   ,2009-2010,       75292
    5.0   ,2009-2010,       7555
    6.0   ,2009-2010,       9185
    2.0   ,2010-2011,       2073
    3.0   ,2010-2011,       582
    4.0   ,2010-2011,       6248
    5.0   ,2010-2011,       215
    6.0   ,2010-2011,       9153
    7.0   ,2010-2011,       3831
    3.5   ,     2011,       5560
    4.5   ,     2011,       1396',header=TRUE,sep=",",stringsAsFactors=FALSE)

# NOT operation is common among the conditions 

logical= ( !( mydata$Group %in% c(1,2) | grepl('2011',mydata$Year) ) )
mydata_wk<-mydata[logical,]

> mydata_wk
   Group      Year Height
9      3 2007-2008   7450
10     4 2007-2008    234
13     3 2008-2009   2606
14     4 2008-2009    519
15     5 2008-2009  54857
18     3 2009-2010   4792
19     4 2009-2010  75292
20     5 2009-2010   7555
21     6 2009-2010   9185

> str(mydata_wk)
'data.frame':   9 obs. of  3 variables:
 $ Group : num  3 4 3 4 5 3 4 5 6
 $ Year  : chr  "2007-2008" "2007-2008" "2008-2009" "2008-2009" ...
 $ Height: int  7450 234 2606 519 54857 4792 75292 7555 9185
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top