I have measured values for 47 chemicals in a stream. After processing
the original data frame through reshape2, the recast data frame has this
structure:
'data.frame': 256 obs. of 47 variables:
$ site : Factor w/ 143 levels "BC-0.5","BC-1",..: 1 1
1 2 2 2 2 2 2 2
...
$ sampdate : Date, format: "1996-04-19" "1996-05-21" ...
$ Acid : num NA NA NA NA NA NA NA NA NA NA ...
$ Ag : num NA NA NA NA NA NA NA NA NA NA ...
$ Al : num 0.07 NA NA NA NA NA NA NA NA NA ...
$ Alk-HO : num NA NA NA NA NA NA NA NA NA NA ...
$ Alk-Tot : num 162 152 212 NA NA NA NA NA NA NA ...
$ As : num 0.01 NA NA 0 0 0 0 0.01 0 0.01 ...
$ Ba : num 0.18 NA NA NA NA NA NA NA NA NA ...
$ Be : num NA NA NA NA NA NA NA NA NA NA ...
$ Bo : num NA NA NA NA NA NA NA NA NA NA ...
$ CO3 : num NA NA NA NA NA NA NA NA NA NA ...
$ Ca : num 76.6 NA NA NA NA ...
$ Cd : num NA NA NA NA NA NA NA NA NA NA ...
$ Cl : num 12 NA NA NA NA NA NA NA NA NA ...
$ Cn : num NA NA NA NA NA NA NA NA NA NA ...
$ Co : num NA NA NA NA NA NA NA NA NA NA ...
$ Cond : num 712 403 731 NA NA NA NA NA NA NA ...
$ Cr : num NA NA NA NA NA NA NA NA NA NA ...
$ DO : num NA NA NA NA NA NA NA NA NA NA ...
$ F : num NA NA NA NA NA NA NA NA NA NA ...
$ Fe : num 0.06 NA NA NA NA NA NA NA NA NA ...
$ Flow : num NA NA NA NA NA NA NA NA NA NA ...
$ HCO3 : num 162 152 212 NA NA NA NA NA NA NA ...
$ Hg : num 0 NA NA NA NA NA NA NA NA NA ...
$ K : num 1.7 NA NA NA NA NA NA NA NA NA ...
$ Mg : num 43.2 NA NA NA NA ...
$ Mn : num NA NA NA NA NA NA NA NA NA NA ...
$ NO2-N : num NA NA NA NA NA NA NA NA NA NA ...
$ NO3-N : num NA 0.47 0.09 NA NA NA NA NA NA NA ...
$ NO3-NO2-N: num 1.97 NA NA NA NA NA NA NA NA NA ...
$ Na : num NA NA NA NA NA NA NA NA NA NA ...
$ Ni : num NA NA NA NA NA NA NA NA NA NA ...
$ OH : num NA NA NA NA NA NA NA NA NA NA ...
$ P : num 0.03 NA NA NA NA NA NA NA NA NA ...
$ Pb : num NA NA NA NA NA NA NA NA NA NA ...
$ SO4 : num 175 57 194 NA NA NA NA NA NA NA ...
$ Sb : num 0 NA NA NA NA NA NA NA NA NA ...
$ Se : num 0.01 NA NA NA NA NA NA NA NA NA ...
$ Si : num NA NA NA NA NA NA NA NA NA NA ...
$ TDS : num 460 212 530 NA NA NA NA NA NA NA ...
$ TSS : num NA 26 NA NA NA NA NA NA NA NA ...
$ Temp : num NA NA NA NA NA NA NA NA NA NA ...
$ Tl : num NA NA NA NA NA NA NA NA NA NA ...
$ Turb : num 2.2 NA NA NA NA NA NA NA NA NA ...
$ Zn : num 0.02 NA NA NA NA NA NA NA NA NA ...
$ pH : num 8.12 8.19 8.46 NA NA NA NA NA NA NA ...
I want a subset of this with only 7 chemicals: Ca, Cl, Cond, Mg, Na, SO4,
and TDS. The subset help page tells me that I can use a logical subset to
extract these 7 rows while keeping all columns, but I do not know how to
write that logical subset. I tried emulating the example on the help page of
avoiding the subset but R didn't like the '%in%' as I wrote it;
putting the
desired row names in a subset vector fails:
burns.tds <- subset(burns.cast, subset(c('Ca', 'Cl',
'Cond', 'Mg', 'Na',
'SO4', 'TDS')))
Error in subset.default(c("Ca", "Cl", "Cond",
"Mg", "Na", "SO4", "TDS")) :
argument "subset" is missing, with no default
What is the proper syntax to extract only these rows into a new data
frame? And, is the recast data frame the appropriate format as the source?
Rich
On Wed, 2 Nov 2011, Rich Shepard wrote:> I want a subset of this with only 7 chemicals: Ca, Cl, Cond, Mg, Na, SO4, > and TDS.I should have also written that what I ultimately want is to create a box-and-whisker plot for these 7 chemicals in a single panel. If that can be done directly from the source data frame without creating another subset, I want to learn the syntax for that. Rich
Seeliger.Curt at epamail.epa.gov
2011-Nov-02 16:57 UTC
[R] Proper Syntax for Logical Subset in Subset()
> I have measured values for 47 chemicals in a stream. After processing > the original data frame through reshape2, the recast data frame has this > structure: > > 'data.frame': 256 obs. of 47 variables: > $ site : Factor w/ 143 levels "BC-0.5","BC-1",..: 1 1 1 2 2 2 2 22 2...> $ sampdate : Date, format: "1996-04-19" "1996-05-21" ... > $ Acid : num NA NA NA NA NA NA NA NA NA NA ......> > I want a subset of this with only 7 chemicals: Ca, Cl, Cond, Mg, Na,SO4,> and TDS. The subset help page tells me that I can use a logical subsetto> extract these 7 rows while keeping all columns, but I do not know how to > write that logical subset.Wow, I don't see how you wound up with the code you tried based on the instructions. I guess for some of us the best instruction is a bloody nose, myself often included. It sounds like you want to get rid of some columns in the reshaped dataframe, not rows. new <- old[c('site', 'Ca', 'Cl', 'Cond', 'Mg', 'Na', 'SO4', 'TDS')] will work, as will the following if you insist on using the subset function. new <- subset(old, c(site, Ca, Cl, Cond, Mg, Na, SO4, TDS)) If you actually want to get rid of rows that aren't the minerals of interest, then work with your unreshaped dataframe: new <- subset(reallyOld, parameter in c('Ca', 'Cl', 'Cond', 'Mg', 'Na', 'SO4', 'TDS')) -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.curt@epa.gov 541/754-4638 [[alternative HTML version deleted]]