For the data you provide, it's simply:

summary(subset(dat, x1 == "x" & x2 == "z")$y)

Note that x1 and x2 are factors in your example.

We also don't know what you want to do if there are more than one
combination of that per ID, or if there ID values with no matching
rows.

Sarah

On Fri, Oct 14, 2016 at 2:26 PM, Ashta <sewa...@gmail.com> wrote:
> Hi all,
>
> I am trying to summarize  big data set  by   selecting a row
> conditionally. and tried  to do it in a loop
>
> Here is  the sample of my data and my attempt
>
> dat<-read.table(text=" ID,x1,x2,y
> 1,a,b,15
> 1,x,z,21
> 1,x,b,16
> 1,x,k,25
> 2,d,z,31
> 2,x,z,28
> 2,g,t,41
> 3,h,e,32
> 3,x,z,38
> 3,x,g,45
> ",sep=",",header=TRUE)
>
> For  each unique ID,  I want to select  a data when x1= "x" and x2="z"
> Here is the selected data (newdat)
> ID,x1,x2,y
> 1,x,z,21
> 2,x,z,28
> 3,x,z,38
>
> Then I want summarize  Y values and out put as follows
> Summerize
> summary(newdat[i])
> ######################################################
> ID   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
> 1
> 2
> 3
> .
> .
> .
> 28
> ####################################################
>
> Here is my attempt but did not work,
>
> trt=c(1:28)
> for(i  in 1:length (trt))
> {
>   day[i]= newdat[which(newdat$ID== trt[i] &  newdat$x1 =="x" &
> newdat$x2 =="z"),]
> NR[i]=dim(day[i])[1]
> print(paste("Number of Records      :", NR[i]))
> sm[i]=summary(day[i])
> }
>
> Thank you in advance
>

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to