[R] Newbie: Selecting data

Francisco J. Zagmutt gerifalte28 at hotmail.com
Thu Oct 19 23:31:50 CEST 2006


Welcome to the R community!
I see you already learned some of the data selection methods using [].  You 
can use the same principles to subset data  i.e.


dat=read.table("clipboard") #reading the data you posted, after copied to my 
clipboard
dat
      start therms   gas KWHs elect temp days
1 10-Jun-98      9 16.84  613 63.80   75   40
2 20-Jul-98      6 15.29  721 74.21   76   29
3 18-Aug-98      7 15.73  597 62.22   76   29
4 16-Sep-98     42 35.81  460 43.98   70   33
5 19-Oct-98    105 77.28  314 31.45   57   29
6 17-Nov-98    106 77.01  342 33.86   48   30

subDat=dat[dat$temp<65,] #Subsetting only days with temp<65.  Notice the use 
of commas and $ here

subDat
      start therms   gas KWHs elect temp days
5 19-Oct-98    105 77.28  314 31.45   57   29
6 17-Nov-98    106 77.01  342 33.86   48   30

You can also use the higher level function subset i.e.

subDat=subset(dat, temp <65)
subDat
      start therms   gas KWHs elect temp days
5 19-Oct-98    105 77.28  314 31.45   57   29
6 17-Nov-98    106 77.01  342 33.86   48   30

I would encourage you to invest some time reading the pdf manual available 
in the main console under Help->Manuals (in pdf) -> An Introduction to R

I hope this helps

Francisco

Dr. Francisco J. Zagmutt
College of Veterinary Medicine and Biomedical Sciences
Colorado State University




>From: "Zembower, Kevin" <kzembowe at jhuccp.org>
>To: <r-help at stat.math.ethz.ch>
>Subject: [R] Newbie: Selecting data
>Date: Thu, 19 Oct 2006 16:12:54 -0400
>
>I've been working with R for all of about 8 hours, so anyone with more
>experience than this should be able to help me. General comments about
>my methods of work are also welcomed.
>
>I have a table that I've imported thusly:
> > w <- read.table("woodford.data", header=T)
> > w
>        start therms    gas KWHs  elect temp days
>1  10-Jun-98      9  16.84  613  63.80   75   40
>2  20-Jul-98      6  15.29  721  74.21   76   29
>3  18-Aug-98      7  15.73  597  62.22   76   29
>4  16-Sep-98     42  35.81  460  43.98   70   33
>5  19-Oct-98    105  77.28  314  31.45   57   29
>6  17-Nov-98    106  77.01  342  33.86   48   30
><snip>
>
>[This is real data on my house.] 'days' is number of days in bill,
>'temp' is average temperature in Fahrenheit. I'd like to see if there is
>a relationship between the gas burned (therms) and the number of heating
>degree days.
>
>I compute therms per day and heating degree days like this:
> > thermsperday <- w[,2]/w[,7]
> > hdd <- (w[,6] -65)*w[,7]
>
>However, I only want the data for the months in which the average
>temperature is less than 65 (otherwise, it's a cooling degree day). I
>tried ifelse, but couldn't get it to work. What simple technique am I
>overlooking?
>
>Thanks so much for your help and suggestions, especially for your
>patience with a newbie.
>
>-Kevin
>
>Kevin Zembower
>Internet Services Group manager
>Center for Communication Programs
>Bloomberg School of Public Health
>Johns Hopkins University
>111 Market Place, Suite 310
>Baltimore, Maryland  21202
>410-659-6139
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide 
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

_________________________________________________________________
Get today's hot entertainment gossip



More information about the R-help mailing list