0

I am trying to plot a graph for a data containing years between 1900 and 2010 and output in each month of the year in R. I need to select years between 1950-2001 against months of Nov-february. How can I select part of data for plotting this graph ? since I am a rookie at R programming or any programming, an easy to follow example would be of great help.

thanks GRV

  • 4
    Please read [How to make a great R reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – zero323 Oct 27 '13 at 22:30
  • 2
    check the function `subset`, and notice that `plot` can use a `subset` argument that works in the same way as said function. also, follow zero323's advice. – Juan Oct 27 '13 at 22:33
  • Hi and welcome to stackoverflow! You are much more likely to receive a helpful answer if you show what you have tried, explain why your attempts didn't work, and clearly describe your desired results/outputs. Cheers. – Henrik Oct 27 '13 at 22:53

1 Answers1

1

I am not sure exactly what you mean by

select years between 1950-2001 against months of Nov-february

But the following should get you started on a reproducible example...

#create a vector of months from 1900 through 2010
months <- seq(as.Date("1900/1/1"), as.Date("2010/12/31"), "months")
#assign a random vector of equal length
output <- rnorm(length(months))
#assign both values to a data_frame
data <- data.frame(months = months, output = output)

Based on your description, your data should look something like the dataframe, called data.

From here, you can make use of the subset function to help you on your way. The first example subsets to data from 1950 through 2001. The next further restricts that subset to the months of November through February.

#subset to just 1950 through 2001
data_sub <- subset(data, months >= as.Date("1950-01-01") & months <= as.Date("2001-12-31"))
#subset the 1950 to 2001 data to just Nov-feb months (i.e. c(11,12,1,2))
data_sub_nf <- subset(data_sub, as.numeric(format(data_sub$months, "%m")) %in% c(11,12,1,2))

You should also read Why is `[` better than `subset`? to move beyond subset.

As stated, after the data has been subset, you can use plot or any other plotting function to graph your data.

Community
  • 1
  • 1
joemienko
  • 2,220
  • 18
  • 27
  • Thank you very much @Joemienko . I followed following method to create subsets Nov<-as.numeric(snow$Nov) Dec<-as.numeric(snow$Dec) Jan<-as.numeric(snow$Jan) Feb<-as.numeric(snow$Feb) Mar<-as.numeric(snow$Mar) month<-cbind(Nov,Dec,Jan,Feb,Mar) year<-as.numeric(substr(snow$Season,1,4)) data<-as.data.frame(cbind(year,month)) data<-data[order(data$year,decreasing=TRUE),] data1<-subset(data,(year>=1950 & year<=2001),select=c(year,Nov,Dec,Jan,Feb,Mar)) – user2926175 Oct 28 '13 at 18:50
  • You are quite welcome! Would you kindly accept the answer if it was helpful? – joemienko Oct 29 '13 at 22:52