Subsetting in R

6 views
Skip to first unread message

James, Cassandra

unread,
Sep 10, 2012, 10:49:54 PM9/10/12
to tropi...@googlegroups.com

Hi All,

 

A small R conundrum that I am sure I should be able to solve but always seem to get stuck when it comes to subsetting data based on information in another column of the same dataframe.

 

I have two columns of data (HydroID and NextDownID). I want to identify all the values for HydroID that DONT occur in NextDownID.

 

(basically this is to identify all the river network heads to start an accumulation of runoff downstream).

 

Any help appreciated!

 

Thanks

 

Cassie

Phillips, Ben

unread,
Sep 11, 2012, 12:18:26 AM9/11/12
to <tropical-r@googlegroups.com>
Hey Cassie,

You need the within operator: %in%


B


Excuse the brevity: sent from my phone.
--
An R group for questions, tips and tricks relevant to spatial ecology and climate change.
All R questions welcome.
---
You received this message because you are subscribed to the Google Groups "Tropical R" group.
To post to this group, send an email to tropi...@googlegroups.com.
To unsubscribe from this group, send email to tropical-r+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

stewartmacdonald

unread,
Sep 11, 2012, 12:22:29 AM9/11/12
to tropi...@googlegroups.com, cassand...@jcu.edu.au
I'm guessing that my last message was also flagged as spam, so I'll try posting this from the website.

Does this work?



HydroID <- c(1, 2, 3, 4, 5, 6)

NextDownID <- c(5, 6, 7, 8, 9, 10)

df <- data.frame(HydroID, NextDownID)


df$HydroID[!(df$HydroID %in% df$NextDownID)]



Stewart

Stewart Macdonald

unread,
Sep 10, 2012, 11:03:53 PM9/10/12
to tropi...@googlegroups.com, Cassandra James
Does this work?


HydroID <- c(1, 2, 3, 4, 5, 6)
NextDownID <- c(5, 6, 7, 8, 9, 10)
df <- data.frame(HydroID, NextDownID)

df$HydroID[!(df$HydroID %in% df$NextDownID)]


Stewart


Reply all
Reply to author
Forward
0 new messages