有執行此函數read.table時不正確導入幾個值:有分裂列更有效的方式
hs.industry <- read.table("https://download.bls.gov/pub/time.series/hs/hs.industry", header = TRUE, fill = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
具體而言,有在industry_code和industry_name結合在一起形成幾個值industry_code列中的單個值(不知道爲什麼)。由於每industry_code是4個位數,我的做法分裂和正確的是:
for (i in 1:nrow(hs.industry)) {
if (isTRUE(nchar(hs.industry$industry_code[i]) > 4)) {
hs.industry$industry_name[i] <- gsub("[[:digit:]]","",hs.industry$industry_code[i])
hs.industry$industry_code[i] <- gsub("[^0-9]", "",hs.industry$industry_code[i])
}
}
我覺得這是非常innificent,但我不知道用什麼辦法會更好。
謝謝!
謝謝!你能否解釋崩潰的必要性? – Michael
當您使用帶有「text」參數的read.table'時,文本必須是單個字符串,而不是字符串列表。因此,我們用換行符摺疊字符串列表(其中每個項目代表原始文本的一行)。 – jdobres