删除R中两个字符串中的共享字符

时间:2015-06-27 16:42:01

标签: r character

我目前有两个字符串(newIV和diffname),如下所示:

> newIV
[1] "i1"  "i2"  "i3"  "i4"  "i5"  "i6"  "i7"  "i8"  "i9"  "i10"
[11] "i11" "i12" "i13" "i14" "i15" "i16" "i17" "i18" "i19" "i20"
[21] "i21" "i22" "i23" "i24" "i25" "i26" "i27" "i28" "i29" "i30"
[31] "i31" "i32" "i33" "i34" "i35" "i36" "i37" "i38" "i39" "i40"
[41] "i41" "i42" "i43" "i44" "i45" "i46" "i47" "i48" "i49" "i50"
[51] "i51" "i52" "i53" "i54" "i55" "i56" "i57" "i58" "i59" "i60"
[61] "i61" "i62" "i63" "i64" "i65" "i66" "i67" "i68" "i69" "i70"
[71] "i71" "i72" "i73" "i74" "i75" "i76" "i77" "i78" "i79" "i17"
[81] "i19" "i20" "i21" "i63" "i75"

> diffname
[1] "i17" "i19" "i20" "i21" "i63" "i75"

我想自动化该过程,以便newIV仅包含diffname中找不到的字符。我尝试过以下方法:

newIV<-newIV[-which(newIV==diffname)]

然而,当我尝试这个时,我仍然会收到错误。

Warning message:
In newIV == diffname :
longer object length is not a multiple of shorter object length

有什么想法?谢谢!

1 个答案:

答案 0 :(得分:3)

尝试%in%

 newIV[!newIV %in% diffname]
 #[1] "i1"  "i2"  "i3"  "i4"  "i5"  "i6"  "i7"  "i8"  "i9"  "i10" "i11" "i12"
 #[13] "i13" "i14" "i15" "i16" "i18" "i22" "i23" "i24" "i25" "i26" "i27" "i28"
 #[25] "i29" "i30" "i31" "i32" "i33" "i34" "i35" "i36" "i37" "i38" "i39" "i40"
 #[37] "i41" "i42" "i43" "i44" "i45" "i46" "i47" "i48" "i49" "i50" "i51" "i52"
 #[49] "i53" "i54" "i55" "i56" "i57" "i58" "i59" "i60" "i61" "i62" "i64" "i65"
 #[61] "i66" "i67" "i68" "i69" "i70" "i71" "i72" "i73" "i74" "i76" "i77" "i78"
#[73] "i79"

或者

 setdiff(newIV, diffname)
 #[1] "i1"  "i2"  "i3"  "i4"  "i5"  "i6"  "i7"  "i8"  "i9"  "i10" "i11" "i12"
 #[13] "i13" "i14" "i15" "i16" "i18" "i22" "i23" "i24" "i25" "i26" "i27" "i28"
 #[25] "i29" "i30" "i31" "i32" "i33" "i34" "i35" "i36" "i37" "i38" "i39" "i40"
 #[37] "i41" "i42" "i43" "i44" "i45" "i46" "i47" "i48" "i49" "i50" "i51" "i52"
 #[49] "i53" "i54" "i55" "i56" "i57" "i58" "i59" "i60" "i61" "i62" "i64" "i65"
 #[61] "i66" "i67" "i68" "i69" "i70" "i71" "i72" "i73" "i74" "i76" "i77" "i78"
 #[73] "i79"