2014-09-19 308 views
0

尝试合并两个数据帧,使用名为hash_id的变量。出于某种原因,R不能识别其中一个数据帧中的哈希ID,而在另一个数据帧中则是这样。WeiRd:R找不到值,但它只是

我查过了,我就是不明白。请参阅下面我如何检查:

> 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 
# 
+1

可以用'dput'您展示一些重复的例子。例如。 'dput(head(df1,20)'或子集关联的行并输入它。另外,如果它是一个有多列的数据帧,最好使用'df1 [,“colName”] =='abab123123'' – akrun 2014-09-19 08:16:23

回答

1

一种可能性是在数据集的一个有关列trailingleading空间。你可以这样做:

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) 

另一种方法是使用grep

grepl("\\babab123123\\b", df1[,"hash_id"]) 
grepl("\\babab123123\\b", df2[,"hash_id"])   
+0

确实!非常感谢!我使用了apply:df_trimmed < - as.data.frame(apply(df,2,function(x)sub(“\\ s + $”,“”,x))来修整整个df中的尾部空格。 )) - - 信用:http://stackoverflow.com/a/2261149/293623和http://stackoverflow.com/a/20760767/293623 – 2014-09-19 09:53:37

+1

@蒂姆亨尼斯我会用'df [] < - lapply(df, str_trim)或你提到的'sub'函数使用'apply'会让你陷入麻烦,如果这些列是'multiple'类的, – akrun 2014-09-19 09:59:37

+0

谢谢。我会尽力的,但我似乎没有遇到麻烦,尽管数据框包含来自不同类的列。 – 2014-09-21 10:12:02

相关问题