sorry ralph. i meant the OR instead of the AND so that was my bad
mistake. the subset function should also work with the OR.
i think i understand better what you want now also. the approach below
for doing what you want assumes that , if there are 2 rows associated
with the
values in the first 2 columns , then they will be 1 and 2. If they are
1,1 or 2,2, then it won't work. So, henrique's solution could be better
and more general.
Assume your dataframe is called DF.
tempres<-split(DF$x,DF$y)
onlytwo<-lapply(tempres, function(.df)
if (nrow(.df) == 2) {
return(.df) } else {
return(NULL) }
)
onlytwo<-onlytwo[!sapply(onlytwo,is.null)
result<-do.call(rbind,onlytwo)
On Wed, Aug 13, 2008 at 2:45 PM, Ralph S. wrote:
I tried this - I get an empty set:
<0 rows> (or 0-length row.names)
I guess this happens because the z variable takes only one value per
row??
What works is:
DFsub<-DF[DF$z == 1 | DF$z == 2,]
but then, I do not eliminate the entries where there is only one entry
for z given an a and c combination.
Any idea what to do?
-Ralph
> Date: Wed, 13 Aug 2008 13:05:25 -0500 From: [EMAIL PROTECTED]
> Subject: RE: [R] subsetting matrix according to columns with character
> index To: [EMAIL PROTECTED]
> it must be a dataframe so, if it was DF, then, assuming i understand
> what you want then either of the following should work:
> DFsub<-DF[DF$z == 1 & DF$z == 2,]
> or
> DFsub<-subset(DF, z == 1 & z == 2 )
>
> On Wed, Aug 13, 2008 at 2:00 PM, Ralph S. wrote:
>> Hi,
>> I have a long matrix of the following form which I would like to
>> subset according to the third column:
>> [x y z]:
>> a1 c1 1 a1 c1 2 a2 c1 1 a1 c2 1 a1 c2 2 . . .
>>
>> The first two columns a characters ai and cj.
>> I would like to keep all the rows where there are two entries for z,
>> 1 and 2.
>> That is, I want: a1 c1 1 a1 c1 2 a1 c2 1 a1 c2 2 . . .
>> I try to use something like df[by(df,c(df$x,df$y),sum(z)==3),] but
>> that only gives me one line of data per x y combination.
>> Is there an easy way of coding to keep all rows for a and c
>> combinations where z has entries both 1 and 2? Many thanks,
>> Ralph
>> _________________________________________________________________
>>
>> LM_WLYIA_whichathlete_us
>> ______________________________________________ [email protected]
>> 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.
___________________________________
Your PC, mobile phone, and online services work together like never
before. See how Windows® fits your life
<http://clk.atdmt.com/MRT/go/108587394/direct/01/>
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.