从R中的字母数字列中删除字符?

时间:2016-09-21 10:04:09

标签: r text-mining tm data-cleansing

我正在寻找一个代码来从数据框的字母数字向量中删除字符。这是我的数据栏目:

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

1 个答案:

答案 0 :(得分: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