Re: [D-RUG]: Find index?

8 views
Skip to first unread message
Message has been deleted

Ryan Peek

unread,
Sep 29, 2017, 11:42:55 AM9/29/17
to davi...@googlegroups.com
Should be able to use:
 with(data, name[x==3 & y==32])

Or with col numbers:
data[data$x==3 & data$y==32, 1 ]

Sent from my iPhone

On Sep 29, 2017, at 8:30 AM, bic ton <bict...@gmail.com> wrote:

Hi,

If I have this simple data:

> data
           name        x    y 
         lund        3   32  
         malmo    24  6   
       germn      3   81    

Now to show the name where x=3 and y=32
with one condition we can do this:
with(data, name[x==3])
[1] lund
Levels: Ago.......

but with two conditions any idea  show the name where x has to equal 3 and y has to equal 32?
Bic

--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at https://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Evan Batzer

unread,
Sep 29, 2017, 11:44:08 AM9/29/17
to davi...@googlegroups.com
Hi Bic,

I think you can just add an "&" here to connect multiple logical statements. I.e:

with(data, name[x==3 & y==32])

PhD Student, Eviner Lab
University of California, Davis

On Fri, Sep 29, 2017 at 8:30 AM, bic ton <bict...@gmail.com> wrote:
Hi,

If I have this simple data:

> data
           name        x    y 
         lund        3   32  
         malmo    24  6   
       germn      3   81    

Now to show the name where x=3 and y=32
with one condition we can do this:
with(data, name[x==3])
[1] lund
Levels: Ago.......

but with two conditions any idea  show the name where x has to equal 3 and y has to equal 32?
Bic

--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+unsubscribe@googlegroups.com.

Vince S. Buffalo

unread,
Sep 29, 2017, 11:44:33 AM9/29/17
to davi...@googlegroups.com
You might do best doing some type of join. See: http://dplyr.tidyverse.org/reference/join.html

For example:
library(tidyverse)

# create your dataframe (as a tibble)
df <-  tribble(~name,        ~x,   ~y,
                    "lund",        3 ,  32  ,
                    "malmo",    24,  6   ,
                    "germn",      3,   81  ) 

other_df <- tibble(x=3, y=32)

inner_join(df, other_df)
# A tibble: 1 x 3
   name     x     y
  <chr> <dbl> <dbl>
1  lund     3    32

You could also of course do the simple solution:

> df[df$x == 3 & df$y==32, ]
# A tibble: 1 x 3
   name     x     y
  <chr> <dbl> <dbl>
1  lund     3    32

But joins are more extendable to larger problems, more generalizable to special cases, etc.

By the way, in the future – it would be helpful if you can provide the code to produce the dataframe you reference. E.g. with tribble as I did above, or the classic way with data.frame. See: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

Vince


On Fri, Sep 29, 2017 at 8:30 AM, bic ton <bict...@gmail.com> wrote:
Hi,

If I have this simple data:

> data
           name        x    y 
         lund        3   32  
         malmo    24  6   
       germn      3   81    

Now to show the name where x=3 and y=32
with one condition we can do this:
with(data, name[x==3])
[1] lund
Levels: Ago.......

but with two conditions any idea  show the name where x has to equal 3 and y has to equal 32?
Bic

--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.



--
Vince Buffalo
@vsbuffalo :: vincebuffalo.com
Coop Lab :: Population Biology Graduate Group
University of California, Davis
Reply all
Reply to author
Forward
Message has been deleted
0 new messages