-4

I have the following data with the ID of subjects.

V1
1   2
2   2
3   2
4   2
5   2
6   2
7   2
8   2
9   2
10  2
11  2
12  2
13  2
14  2
15  2
16  4
17  4
18  4
19  4
20  4
21  4
22  4
23  4
24  4

I want to subset all the rows of the data where V1 == 4. This way I can see which observations relate to subject 4.

For example, the correct output would be

16  4
17  4
18  4
19  4
20  4
21  4
22  4
23  4
24  4

However, the output I'm given after subsetting does not give me the correct rows . It simply gives me.

V1
1  4
2  4
3  4
4  4
5  4
6  4
7  4
8  4

I'm unable to tell which observations relate to subject 4, as observations 1:8 are for subject 2.

I've tried the usual methods, such as

condition<- df == 4
df[condition]

How can I subset the data so I'm given back a dataset that shows the correct row numbers for subject 4.

user3200293
  • 181
  • 5
  • 18

2 Answers2

1

You can also use the subset function:

subset(df,df$V1==4)
Ansjovis86
  • 1,506
  • 5
  • 17
  • 48
0

I've managed to find a solution since posting.

newdf <- subset(df, V1 == 4). 

However i'm still very interested in other solutions to this problems, so please post if you're aware of another method.

user3200293
  • 181
  • 5
  • 18
  • you shouldn't use `R` objects as names for user created objects: you should call your data.frame something other than `df`. – stas g Nov 11 '15 at 13:29
  • 4
    @stasg, this is _very_ commonly done in R questions on SO.. – talat Nov 11 '15 at 13:30
  • 1
    @docendodiscimus yes, i know :( – stas g Nov 11 '15 at 13:31
  • the object name `df` is for the sake of clarity. My actual data.frame is not called this. – user3200293 Nov 11 '15 at 13:32
  • you probably shouldn't use `R` object names for any reason. there are many alternatives `df1`, `df_`, etc.. – stas g Nov 11 '15 at 13:33
  • 1
    @stasg that argument is true in a general sense, but shouldn't be compared to using `c`, `t`, or `str` as a name. Those are especially problematic because they are commonly used. When was the last time you used `df`? You're right that it is suboptimal but it is very quick and useful for examples. – Pierre L Nov 11 '15 at 13:43
  • @PierreLafortune true enough -- i don't recall last time i used `df`! however, i guess, not using _any_ `R` core object names at all would encourage good coding practices and instil certain discipline. – stas g Nov 11 '15 at 14:00
  • 1
    I agree. But I gotta insist on an exception here. It is not only historically used as a default name but actually works well. It is likened to the slang that has become common usage. For example the machine with wings that flies through the air was originally called the "aeroplane" to describe the science going on. So many people mistakenly called it "airplane" that eventually it stuck. :) – Pierre L Nov 11 '15 at 14:04
  • @PierreLafortune okay, it is difficult to disagree with this! :) – stas g Nov 11 '15 at 14:09