2016-09-21 10 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), [second] (http://stackoverflow.com/questions/15451251/extract-numeric) 문자열의 일부가 혼합 된 숫자 및 문자), [세 번째] (http://stackoverflow.com/questions/14543627/extraction-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