[R] question about subset
Sean Davis
sdavis2 at mail.nih.gov
Fri May 6 00:45:11 CEST 2005
On May 5, 2005, at 6:31 PM, jose silva wrote:
> rois there any way to get a subset of a data frame with the ordered
> indexes or without these indexes?
>
> example:
>
> mydata<- data.frame(A=seq(1,10), B=c(-5,2,-4,6,-8,-9,2,5,7,0))
> subset(mydata, B>0)
> A B
> 2 2 2
> 4 4 6
> 7 7 2
> 8 8 5
> 9 9 7
>
> I would like to obtain this:
>
> A B
> 1 2 2
> 2 4 6
> 3 7 2
> 4 8 5
> 5 9 7
>
> x <- subset(mydata, B>0)
> row.names(x) <- rank(x$A)
> x
A B
1 2 2
2 4 6
3 7 2
4 8 5
5 9 7
> or this
>
> A B
> 2 2
> 4 6
> 7 2
> 8 5
> 9 7
>
I think you already got this using subset. The row.names can be used
to set the row names to whatever you want.
Sean
More information about the R-help
mailing list