0

Trying to merge two data frames, using a variable called hash_id. For some reason R does not recognize the hash-id's in one of the data frames, while it does so in the other.

I have checked and I just don't get it. See below how I checked:

> head(df1[46],1) # so I take the first 'hash-id' from df1
#    hash_id
# 1 abab123123

> which(df2 == "abab123123", arr.ind=TRUE) # here it shows that row 6847 contains a match
#      row col
# [1,] 6847  32`

> which(df1 == "abab123123", arr.ind=TRUE) # and here there is NO matching value!
#     row col
# 
10 Rep
  • 2,217
  • 7
  • 19
  • 33
Thieme Hennis
  • 565
  • 2
  • 9
  • 20
  • 1
    Could you show some reproducible example using `dput`. For ex. `dput(head(df1,20)` or subset the concerned rows and dput it. Also, if it a dataframe with multiple columns. It is better to use `df1[,"colName"]=='abab123123'` – akrun Sep 19 '14 at 08:16

1 Answers1

1

One possibility is trailing or leading spaces in the concerned columns for one of the datasets. You could do:

library(stringr)
df1[, "hash_id"] <- str_trim(df1[,"hash_id"])
df2[, "hash_id"] <- str_trim(df2[, "hash_id"])

which(df1[, "hash_id"]=="abab123123", arr.ind=TRUE)
which(df2[, "hash_id"]=="abab123123", arr.ind=TRUE)

Another way would be use grep

grepl("\\babab123123\\b", df1[,"hash_id"])
grepl("\\babab123123\\b", df2[,"hash_id"])        
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Indeed! Thanks a lot! I trimmed trailing whitespace in the entire df using apply: `df_trimmed <- as.data.frame(apply(df,2,function (x) sub("\\s+$", "", x)))` - credits: http://stackoverflow.com/a/2261149/293623 and http://stackoverflow.com/a/20760767/293623 – Thieme Hennis Sep 19 '14 at 09:53
  • 1
    @Thieme Hennis I would use `df[] <- lapply(df, str_trim)` or the `sub` function you mentioned. Using `apply` can get you into trouble if the columns are of `multiple` classes. – akrun Sep 19 '14 at 09:59
  • thanks. I will try, but I have not run in trouble it seems, although the dataframe has columns from different classes. – Thieme Hennis Sep 21 '14 at 10:12