[R] Subset and sumerize
Sarah Goslee
sarah.goslee at gmail.com
Fri Oct 14 20:54:28 CEST 2016
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 <sewashm at 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
>
More information about the R-help
mailing list