[R] [FORGED] Grouping Question

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Sun Mar 22 07:59:27 CET 2020


On 22/03/20 4:01 pm, Thomas Subia via R-help wrote:

> Colleagues,
> 
> Here is my dataset.
> 
> Serial	Measurement	Meas_test	Serial_test
> 1	17		fail		fail
> 1	16		pass		fail
> 2	12		pass		pass
> 2	8		pass		pass
> 2	10		pass		pass
> 3	19		fail		fail
> 3	13		pass		pass
> 
> If a measurement is less than or equal to 16, then Meas_test is pass. Else
> Meas_test is fail
> This is easy to code.
> 
> Serial_test is a pass, when all of the Meas_test are pass for a given
> serial. Else Serial_test is a fail.
> I'm at a loss to figure out how to do this in R.
> 
> Some guidance would be appreciated.

In future, please present your data using dput(); makes life much easier 
for those trying to help you.  Your data are really the first two 
columns of what you presented --- the last two columns are your desired 
output.

Let "X" be these first two columns.  Define

foo <- function (X) {
a <- with(X,Measurement <= 16)
a <- ifelse(a,"pass","fail")
b <- with(X,tapply(Measurement,Serial,function(x){all(x<=16)}))
i <- match(X$Serial,names(b))
b <- ifelse(b[i],"pass","fail")
data.frame(Meas_test=a,Serial_test=b)
}

foo(X) gives:

>   Meas_test Serial_test
> 1      fail        fail
> 2      pass        fail
> 3      pass        pass
> 4      pass        pass
> 5      pass        pass
> 6      fail        fail
> 7      pass        fail

If you want input and output combined, as in the way that you presented 
your data use cbind(X,foo(X)).

cheers,

Rolf Turner

-- 
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276



More information about the R-help mailing list