[R] selecting certain rows from data frame
Peter Ehlers
ehlers at ucalgary.ca
Wed Dec 15 09:26:14 CET 2010
On 2010-12-14 23:57, steven mosher wrote:
> Hi,
> Next time give folks code to produce a toy sample of your problem
>
> DF<-data.frame(ID=rep(1:5,each=3),Data=rnorm(15),Stuff=seq(1:15))
> DF
> ID Data Stuff
> 1 1 2.0628225 1
> 2 1 0.6599165 2
> 3 1 0.5672595 3
> 4 2 -0.5308823 4
> 5 2 -0.5358471 5
> 6 2 -0.1414992 6
> 7 3 -0.1679643 7
> 8 3 0.9220922 8
> 9 3 0.8863018 9
> 10 4 -0.7255916 10
> 11 4 -1.2446753 11
> 12 4 0.8165567 12
> 13 5 0.0925008 13
> 14 5 -0.8534803 14
> 15 5 -0.6535016 15
>
> # now I want to select rows where ID = 2 or 5
> # Assign DF2 to those elements of DF where the ID variable=2 or 5
>
> DF2<- DF[which(DF$ID==2 | DF$ID==5), ]
Or use subset():
DF2 <- subset(DF, ID %in% c(2,5))
Peter Ehlers
> DF2
> ID Data Stuff
> 4 2 -0.5308823 4
> 5 2 -0.5358471 5
> 6 2 -0.1414992 6
> 13 5 0.0925008 13
> 14 5 -0.8534803 14
> 15 5 -0.6535016 15
>
> On Tue, Dec 14, 2010 at 10:10 PM, Hrithik R<rithrr at yahoo.com> wrote:
>
>> Hi,
>> if I have a dataframe such that
>>
>> ID Time Earn
>> 1 1 10
>> 1 2 50
>> 1 3 68
>> 2 1 40
>> 2 2 78
>> 2 4 88
>> 3 1 50
>> 3 2 60
>> 3 3 98
>> 4 1 33
>> 4 2 48
>> 4 4 58
>> .....
>> ....
>> .....
>>
>> Now if I have to select the all the rows from the data frame which does not
>> include rows with certain IDs, say for example (prime) ID == 2& 3, how do
>> I do
>> it
>>
>>
>> Thanks
>>
>> Rith
>>
More information about the R-help
mailing list