
抱歉代碼,因為我嘗試輸入一些代碼來生成圖片中的這個 data.frame,但是由于“if”我失敗了。我們知道“if”是 R 中的保留字(像 else,for , while ,function). 而在這里,我特意使用了“if”作為列名(第二列),看看R是否會產生一些新奇的東西。所以使用另一種方式,我在 excel 中輸入“if”并保存為 csv 格式,以便使用read.csv.
問題是:為什么是“如果”。更改為“if”?(在我使用 check.names=FALSE 之后)

uj5u.com熱心網友回復:
?read.csvcheck.names=以類似的方式描述:
check.names: logical. If 'TRUE' then the names of the variables in the
data frame are checked to ensure that they are syntactically
valid variable names. If necessary they are adjusted (by
'make.names') so that they are, and also to ensure that there
are no duplicates.
默認操作是允許您執行類似的操作dat$<column-name>,但不幸的是dat$if會失敗Error: unexpected 'if' in "dat$if",因此check.names=TRUE將其更改為決議器不會絆倒的內容。但是請注意,即使dat[["if"]]不會,這也會起作用dat$if。
如果你想知道,如果check.names=FALSE是以往任何時候都糟糕的事情,那么想象一下:
dat <- read.csv(text = "a,a\n2,3")
dat
# a a.1
# 1 2 3
dat <- read.csv(text = "a,a\n2,3", check.names = FALSE)
dat
# a a
# 1 2 3
在第二種情況下,如何按名稱訪問第二列?只dat$a回傳2。但是,如果您不想使用$or [[,而是可以依賴于列的位置索引,那么dat[,colnames(dat) == "a"]確實會回傳它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/343024.html
上一篇:計算每個站點的新因子水平(R)
