compare two characters based on subset
Posted
by
schultem
on Stack Overflow
See other posts from Stack Overflow
or by schultem
Published on 2012-09-04T21:21:55Z
Indexed on
2012/09/04
21:38 UTC
Read the original article
Hit count: 130
I have a simple dataframe with two columns:
df <- data.frame(x = c(1,1,2,2,3),
y = c(rep(1:2,2),1),
target = c('a','a','a','b','a'))
I would like to compare the strings in the target column (find out whether they are equal or not, i.e., TRUE or FALSE) within every level of x (same number for x). First I would like to compare lines 1 and 2, then 3 and 4 ... My problem is that I am missing some comparisons, for example, line 5 has only one case instead of two - so it should turn out to be FALSE. Variable y indicates the first and second case within x.
I played around with ddply doing something like:
ddply(df, .(x), summarise,
ifelse(as.character(df[df$y == '1',]$target),
as.character(df[df$y == '2',]$target),0,1))
which is ugly ... and does not work ...
Any insights how I could achieve this comparison?
Thanks
© Stack Overflow or respective owner