怪异:R找不到值,但它';就在那里
尝试使用名为怪异:R找不到值,但它';就在那里,r,merge,R,Merge,尝试使用名为hash\u id的变量合并两个数据帧。由于某些原因,R无法识别其中一个数据帧中的哈希id,而在另一个数据帧中识别 我查过了,就是不明白。请参见下面我是如何检查的: > 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
hash\u id
的变量合并两个数据帧。由于某些原因,R无法识别其中一个数据帧中的哈希id,而在另一个数据帧中识别
我查过了,就是不明白。请参见下面我是如何检查的:
> 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
#
一种可能是其中一个数据集的相关列中的
尾随
或前导
空格。你可以做:
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)
您能否使用
dput
显示一些可复制的示例。例如,dput(head(df1,20)
或相关行的子集并dput它。另外,如果它是一个具有多列的数据帧,则最好使用df1[,“colName”]==“abab123123”
的确如此!非常感谢!我使用apply:df\u trimmed@Thieme Hennis我将使用df[]谢谢。我会尝试的,但我似乎没有遇到麻烦,尽管dataframe有来自不同类的列。
grepl("\\babab123123\\b", df1[,"hash_id"])
grepl("\\babab123123\\b", df2[,"hash_id"])