tags:

views:

51

answers:

3

Using R, I would like to find out which Samples (S1, S2, S3, S4, S5) fulfill the following criteria:contain minimally one value (x, y or z) bigger than 4. Thanks, Alex.

 Sample    x    y    z <br>
     S1 -0.3  5.3  2.5 <br>
     S2  0.4  0.2 -1.2 <br>
     S3  1.2 -0.6  3.2 <br>
     S4  4.3  0.7  5.7 <br>
     S5  2.4  4.3  2.3 <br>
A: 

How does this sound? Copy your data into your clipboard and execute the following commands:

dta <- read.table("clipboard", header = T)
apply(dta[2:4], 1, function(x) ifelse(max(x) >= 4, 1, 0))
Roman Luštrik
+1  A: 

You could try a call to apply - for example:

> apply(dataFrameOfSamples,1,function(x)any(x > 4))
   S1    S2    S3    S4    S5
 TRUE FALSE FALSE  TRUE  TRUE
nullglob
... assuming that Sample is the name of the data frame or matrix rather than just a column of said data frame.
John
A: 

With many rows this could be more efficient:

do.call(pmax, X[c("x","y","z")]) > 4

On your data

ex <- data.frame(
  Sample = c("S1", "S2", "S3", "S4", "S5"),
  x = c(-0.3, 0.4, 1.2, 4.3, 2.4),
  y = c( 5.3, 0.2,-0.6, 0.7, 4.3),
  z = c( 2.5,-1.2, 3.2, 5.7, 2.3)
)

do.call(pmax, ex[c("x","y","z")]) > 4
# [1]  TRUE FALSE FALSE  TRUE  TRUE
Marek