[R] Subset dataframe based on condition

Tony Plate tplate at acm.org
Mon Apr 17 20:46:45 CEST 2006


Works OK for me:

 > x <- data.frame(a=10^(-2:7), b=10^(10:1))
 > subset(x, a > 1)
        a     b
4  1e+01 1e+07
5  1e+02 1e+06
6  1e+03 1e+05
7  1e+04 1e+04
8  1e+05 1e+03
9  1e+06 1e+02
10 1e+07 1e+01
 > subset(x, a > 1 & b < a)
        a    b
8  1e+05 1000
9  1e+06  100
10 1e+07   10
 >

Do you get all "numeric" for the following?

 > sapply(x, class)
         a         b
"numeric" "numeric"
 >

If not, then your data frame is probably encoding the information in 
some way that you don't want (though if it was as factors, I would have 
expected a warning from the comparison operator).

You might get more help by distilling your problem to a simple example 
that can be tried out by others.

-- Tony Plate

Sachin J wrote:
> Hi,
>    
>   I am trying to extract subset of data from my original data frame 
> based on some condition. For example : (mydf -original data frame, submydf 
> - subset dada frame)
>    
>   >submydf = subset(mydf, a > 1 & b <= a), 
>    
>   here column a contains values ranging from 0.01 to 100000. I want to 
> extract only those matching condition 1 i.e a > . But when i execute 
> this command it is not giving me appropriate result. The subset df - 
> submydf  contains rows with 0.01 also. Please help me to resolve this 
> problem.
>    
>   Thanks in advance.
>    
>   Sachin
> 
> 		
> ---------------------------------
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list