[問題敘述]:
我有個程式碼會把外部檔案的檔名存入固定變數,之後要換檔案分析,只要重新
choose.files再
tableName <- strsplit(basename(raw_name), ".", fixed = TRUE)[[1]][1]
> tableName
[1] "midterm_table"
就可以在tableName存入檔名字串,我只要用get(tableName)就可以取代所有
原本主檔名字串(本範例用"midterm_table"),譬如:
sapply(get(tableName), class) %>% unique()
這是成功的。
下面的程式碼,我是要列出表格中的NA,把他改成一個"noData"方便之後計算,
get(tableName)$Other.Injury[is.na(get(tableName)$Other.Injury)]
成功執行,會顯示一整串NA(廢話),但是當我多加後面修改的自串
get(tableName)$Other.Injury[is.na(get(tableName)$Other.Injury)] = "noData"
Error in get(tableName)$Other.Injury[is.na(get(tableName)$Other.Injury)] =
"noData" :
could not find function "get<-"
但是改成字串檔名
midterm_table$Other.Injury[is.na(get(tableName)$Other.Injury)] = "noData"
這樣就成功了。
我不懂那個could not find function "get<-"是怎麼回事,因為其他程式碼都get這樣
也沒問題,何況同一句後面也有用get(...)$,卻唯獨這個語法會出錯。
請問有人知道問題出在哪邊嗎?
[環境敘述]:
R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 LC_CTYPE=Chinese
(Traditional)_Taiwan.950
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950 LC_NUMERIC=C
[5] LC_TIME=Chinese (Traditional)_Taiwan.950
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] openxlsx_4.0.17 magrittr_1.5
loaded via a namespace (and not attached):
[1] compiler_3.4.4 tools_3.4.4 yaml_2.1.14 Rcpp_1.0.0