mode 位於 base 包(package)。 說明
獲取或設置‘mode’(‘type’的一種),或某個對象的存儲模式R對象。
用法
mode(x)
mode(x) <- value
storage.mode(x)
storage.mode(x) <- value
參數
| x | 任何R對象。 | 
| value | 給出對象所需模式或“存儲模式”(類型)的字符串。 | 
細節
mode 和 storage.mode 都返回一個字符串,給出對象的(存儲)模式(通常相同),兩者都依賴於 typeof(x) 的輸出,請參見下麵的示例。
mode(x) <- "newmode" 將對象 x 的 mode 更改為 newmode 。僅當存在適當的 as.newmode 函數時才支持此函數,例如 "logical" ,  "integer" ,  "double" ,  "complex" ,  "raw" ,  "character" ,  "list" ,  "expression" ,  "name" ,  "symbol" 和 "function" 。屬性被保留(但見下文)。
storage.mode(x) <- "newmode" 是 mode<- 的更高效的 primitive 版本,適用於內部類型之一的 "newmode"(請參閱 typeof ),但不適用於 "single" 。屬性被保留。
作為存儲方式"single"僅是pseudo-modeR,不會被報告mode或者storage.mode: 使用attr(object, "Csingle")來檢查這一點。然而,mode<-可用於將模式設置為"single",它將實模式設置為"double"和"Csingle"歸因於TRUE。設置任何其他模式都會刪除此屬性。
請注意(在下麵的示例中)某些 call 具有與 S 兼容的模式 "("。
模式名稱
模式與類型具有相同的名稱集(請參閱 typeof ),除了
- 
類型 "integer"和"double"返回為"numeric"。
- 
類型 "special"、"builtin"和"closure"返回為"function"。
- 
類型 "symbol"稱為模式"name"。
- 
類型 "language"返回為"("或"call"。
例子
require(stats)
sapply(options(), mode)
cex3 <- c("NULL", "1", "1:1", "1i", "list(1)", "data.frame(x = 1)",
  "pairlist(pi)", "c", "lm", "formals(lm)[[1]]",  "formals(lm)[[2]]",
  "y ~ x","expression((1))[[1]]", "(y ~ x)[[1]]",
  "expression(x <- pi)[[1]][[1]]")
lex3 <- sapply(cex3, function(x) eval(str2lang(x)))
mex3 <- t(sapply(lex3,
                 function(x) c(typeof(x), storage.mode(x), mode(x))))
dimnames(mex3) <- list(cex3, c("typeof(.)","storage.mode(.)","mode(.)"))
mex3
## This also makes a local copy of 'pi':
storage.mode(pi) <- "complex"
storage.mode(pi)
rm(pi)
參考
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
也可以看看
typeof 表示 R-internal ‘mode’ 或 ‘type’、type.convert 、attributes 。
相關用法
- R mtfrm 匹配輔助函數
- R merge 合並兩個 DataFrame
- R make.unique 使字符串唯一
- R missing 正式論證有價值嗎?
- R matrix 矩陣
- R matmult 矩陣乘法
- R maxCol 求矩陣中的最大位置
- R match 價值匹配
- R message 診斷信息
- R match.arg 使用部分匹配的參數驗證
- R mat.or.vec 創建矩陣或向量
- R mapply 將函數應用於多個列表或向量參數
- R mean 算術平均值
- R marginSums 計算表格邊距
- R make.names 命名語法上有效的名稱
- R match.call 參數匹配
- R memlimits 查詢和設置堆大小限製
- R match.fun 提取名稱指定的函數
- R memCompress 內存中壓縮和解壓縮
- R memory.profile 分析 Cons 單元的使用情況
- R file.path 構造文件路徑
- R grep 模式匹配和替換
- R getwd 獲取或設置工作目錄
- R vector 向量 - 創建、強製等
- R lapply 對列表或向量應用函數
注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 The (Storage) Mode of an Object。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
