2015-06-27 54 views
0

兩個字符串共享字符我現在有兩個字符串(newIV和diffname)如下:卸下作爲R

> 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 

有什麼想法?謝謝!

回答

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" 
+0

完善該工作就像一個魅力,非常感謝! – costebk08

+2

@akrun:我認爲你提醒OP接受答案是很好的。我在這裏沒有活躍很久,但是當我提供幫助而沒有得到應有的承認時,我發現它真的很煩人,有時甚至沒有評論。 – RHertel

+1

@RHertel感謝您的評論和投票。有時,它很煩人,但並非所有用戶都是這樣。只是幫助你認爲你在幫助某人。你會得到回報的時間。 – akrun