-2
    transaction  | sum
    -------------|--------
 1 | refill      | 1450
 2 | write-off   | -1450
 3 | refill      | 3500

I want to choose only those values that have same absolute value of a number. There are rows 1 and 2. How can I do it in R?

nurma_a
  • 139
  • 8

2 Answers2

1

Another method, using Andre's dataset:

df1<-data.frame(index=1:6,sum=c(1,2,3,-1,3,4))
df1[duplicated(abs(df1$sum)) | duplicated(abs(df1$sum), fromLast = T),]

This is based on scanning for duplicates in forward order, then in reverse order, and then ORing the result to match all duplicated cases. As described here: Find indices of duplicated rows

Mike S
  • 312
  • 1
  • 8
0
df1<-
data.frame(index=1:6,sum=c(1,2,3,-1,3,4))

#  index sum
#1     1   1
#2     2   2
#3     3   3
#4     4  -1
#5     5   3
#6     6   4

keep <- abs(df1$sum) %in% abs(df1$sum[duplicated(abs(df1$sum))])

df1[keep,]

#  index sum
#1     1   1
#3     3   3
#4     4  -1
#5     5   3
Andre Elrico
  • 10,956
  • 6
  • 50
  • 69