This question似乎可以很容易地删除 R 中字符串中的空格字符。但是,当我加载下表时,我无法删除两个数字之间的空格(例如 11 846.4
):
require(XML)
require(RCurl)
require(data.table)
link2fetch = 'https://www.destatis.de/DE/Themen/Branchen-Unternehmen/Landwirtschaft-Forstwirtschaft-Fischerei/Feldfruechte-Gruenland/Tabellen/ackerland-hauptnutzungsarten-kulturarten.html'
theurl = getURL(link2fetch, .opts = list(ssl.verifypeer = FALSE) ) # important!
area_cult10 = readHTMLTable(theurl, stringsAsFactors = FALSE)
area_cult10 = rbindlist(area_cult10)
test = sub(',', '.', area_cult10$V5) # change , to .
test = gsub('(.+)\\s([A-Z]{1})*', '\\1', test) # remove LETTERS
gsub('\\s', '', test[1]) # remove white space?
为什么我不能删除 test[1]
中的空格?
感谢您的任何建议!这可以是空格字符以外的东西吗?也许答案真的很简单,但我忽略了一些东西。
最佳答案
您可以将 test
创建缩短到仅 2 个步骤并仅使用 1 个 PCRE 正则表达式(注意 perl=TRUE
参数):
test = sub(",", ".", gsub("(*UCP)[\\s\\p{L}]+|\\W+$", "", area_cult10$V5, perl=TRUE), fixed=TRUE)
结果:
[1] "11846.4" "6529.2" "3282.7" "616.0" "1621.8" "125.7" "14.2"
[8] "401.6" "455.5" "11.7" "160.4" "79.1" "37.6" "29.6"
[15] "" "13.9" "554.1" "236.7" "312.8" "4.6" "136.9"
[22] "1374.4" "1332.3" "1281.8" "3.7" "5.0" "18.4" "23.4"
[29] "42.0" "2746.2" "106.6" "2100.4" "267.8" "258.4" "13.1"
[36] "23.5" "11.6" "310.2"
gsub
正则表达式值得特别注意:
(*UCP)
- 强制模式识别 Unicode 的 PCRE 动词[\\s\\p{L}]+
- 匹配 1+ 个空格或字母字符|
- 或(交替运算符)\\W+$
- 字符串末尾的 1+ 个非单词字符。
然后,sub(",", ".", x, fixed=TRUE)
将用 替换第一个
作为文字字符串,,
。fixed=TRUE
节省了性能,因为它不必编译正则表达式。
关于html - 删除(不间断的)字符串中的空格字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43734293/