[R] Two conditions selection

Pete Brecknock Peter.Brecknock at bp.com
Wed Aug 5 01:57:48 CEST 2015


Rodrigo Díaz wrote
> Hi. I have a matrix like this: 
> cycle=c(rep(1,3),rep(2,3),rep(3,3),rep(4,3))col=c(rep("blue",2),rep("green",2),rep("blue",2),rep("green",2),rep("blue",2),rep("green",2))values=c(1:12)data.frame(cycle,col,values)
> #  cycle   col values#1      1  blue      1#2      1  blue      2#3      1
> green      3#4      2 green      4#5      2  blue      5#6      2  blue     
> 6#7      3 green      7#8      3 green      8#9      3  blue      9#10    
> 4  blue     10#11     4 green     11#12     4 green     12
> I want to select or extract values matching 2 conditions. For example:
> values from col "blue" and cycle "1". If I use : values[col==blue] I get
> all blue values. I tried using values[c(col==blue,cycle==1)] but is not
> working. Please help. I have a very big data matrix and I do not wanna go
> to excel and start cutting the data. Thanks. 
> 
>  		 	   		  
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________

> R-help@

>  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.


How about 

# Your Code 
cycle=c(rep(1,3),rep(2,3),rep(3,3),rep(4,3))
col=c(rep("blue",2),rep("green",2),rep("blue",2),rep("green",2),rep("blue",2),rep("green",2))
values=c(1:12)
df <- data.frame(cycle,col,values)

# Subset data frame df
df[cycle==1 & col=="blue",]

HTH

Pete



--
View this message in context: http://r.789695.n4.nabble.com/Two-conditions-selection-tp4710762p4710763.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list