Help with creating subset of a data frame

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with creating subset of a data frame

Bhaskar Mitra
Hello Everyone,

I am trying to create a subset of a data frame (df1) based on the first
three
unique values in the first column (v1).

Here are my codes:

b <- unique(df1$v1)[1:3]
df2 <- subset(df1,df1$v1==b)

df1:
v1   v2    v3
1     a    b
1     a1   b1
2     a2   b2
2     a3   b3
3     a4   b4
3     a5   b5
3     a6   b6
4     a7   b7
4     a8   b8
4     a9   b9
5     a10  b10
5     a11  b11
5     a12  b12
5     a13  b13


Ideally, I want my new dataframe (df2) to look something like this:


df2:
v1   v2    v3
1     a    b
1     a1   b1
2     a2   b2
2     a3   b3
3     a4   b4
3     a5   b5
3     a6   b6



However, that doesn't seem to be the case and i am getting the following
warning message:

Warning message:
In df1$v1==b :  longer object length is not a multiple of shorter object
length


I would appreciate any help in this regard,

sincerely,
bhaskar

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with creating subset of a data frame

Bert Gunter-2
%in%  instead of ==

?"%in%"

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Mon, Jul 9, 2018 at 3:35 PM, Bhaskar Mitra <[hidden email]>
wrote:

> Hello Everyone,
>
> I am trying to create a subset of a data frame (df1) based on the first
> three
> unique values in the first column (v1).
>
> Here are my codes:
>
> b <- unique(df1$v1)[1:3]
> df2 <- subset(df1,df1$v1==b)
>
> df1:
> v1   v2    v3
> 1     a    b
> 1     a1   b1
> 2     a2   b2
> 2     a3   b3
> 3     a4   b4
> 3     a5   b5
> 3     a6   b6
> 4     a7   b7
> 4     a8   b8
> 4     a9   b9
> 5     a10  b10
> 5     a11  b11
> 5     a12  b12
> 5     a13  b13
>
>
> Ideally, I want my new dataframe (df2) to look something like this:
>
>
> df2:
> v1   v2    v3
> 1     a    b
> 1     a1   b1
> 2     a2   b2
> 2     a3   b3
> 3     a4   b4
> 3     a5   b5
> 3     a6   b6
>
>
>
> However, that doesn't seem to be the case and i am getting the following
> warning message:
>
> Warning message:
> In df1$v1==b :  longer object length is not a multiple of shorter object
> length
>
>
> I would appreciate any help in this regard,
>
> sincerely,
> bhaskar
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.