Finding unique values in a column based on a condition in another column using r -


given conditional value in 1 column, i'm trying unique list of values in column using 'r'. instance, if input sex = "m", output should list of unique names (john, allan, matt, chris). if input country = us, return list of unique names (john, kate). solutions appreciated!!

country        name      sex             john       m             john       m             kate       f canada         allan      m canada         kate       f canada         matt       m  england        nicole     f germany        kate       f germany        matt       m  germany        chris      m 

if understand correctly, need use subset

you use as

subset(data,sex=="m",select=c("whatever","cols want keep")) 

note if want of them, don't need put select.

and if you've got duplicates, can unique entries running unique() on it.

for data, like...

mydat=read.table("clipboard", header=true) unique(subset(mydat, sex=="m"))    country  name sex 1        john   m 4   canada allan   m 6   canada  matt   m 9  germany  matt   m 10 germany chris   m 

Comments