2016-09-21 90 views
-4

我正在查找代碼以從數據框的字母數字矢量中刪除字符。這是我的數據下面列:從R中的字母數字列中刪除字符?

F9667968CU 
67968PX11 
3666SP 
6SPF10 
2323DL1 
23DVL10 
2016PP07 

而且這是我使用的代碼:

for(i in 1: length(rownames(testsample))) 
{ 
    testsample$column1[i]<-gsub("[a-zA-Z]","",testsample$column1[i]) 
} 

而下面是我期待的輸出:

9667968 
6796811 
3666 
610 
23231 
2310 
201607 
+4

真的嗎? [首先谷歌命中](http://stackoverflow.com/questions/17009628/extracting-numbers-from-string-in-r),[第二](http://stackoverflow.com/questions/15451251/extract-numeric - 部分混合字符串和字符在r),[第三](http://stackoverflow.com/questions/14543627/extracting-numbers-from-vectors-of-strings) – Sotos

+1

'gsub'被矢量化,你不需要for循環,'testsample $ column1 < - gsub(「[a-zA-Z]」,「」,testsample $ column1)''應該可以工作 – Cath

回答

0

試試這個:

df 
     col 
1 F9667968CU 
2 67968PX11 
3  3666SP 
4  6SPF10 
5 2323DL1 
6 23DVL10 
7 2016PP07 

df$col <- as.integer(gsub('[a-zA-Z]', '', df$col)) 
df 
     col 
1 9667968 
2 6796811 
3 3666 
4  610 
5 23231 
6 2310 
7 201607