矩阵转换成数据框
> library(xlsx)
> setwd("C:/Users/Administrator/Desktop/R/RData")
> cars32 <- read.xlsx("mtcars.xlsx",sheetIndex = 1,header = T)
> is.data.frame(cars32)
[1] TRUE
> is.data.frame(state.x77)
[1] FALSE
> dstate.x77 <- as.data.frame(state.x77)
> is.data.frame(dstate.x77)
[1] TRUE
数据框转换成矩阵,如果数据框中既有数值也有字符串,转化成矩阵后,数值会变成字符串。
> head(as.matrix(data.frame(state.region,state.x77)))state.region Population Income Illiteracy Life.Exp Murder HS.Grad Frost
Alabama "South" " 3615" "3624" "2.1" "69.05" "15.1" "41.3" " 20"
Alaska "West" " 365" "6315" "1.5" "69.31" "11.3" "66.7" "152"
Arizona "West" " 2212" "4530" "1.8" "70.55" " 7.8" "58.1" " 15"
Arkansas "South" " 2110" "3378" "1.9" "70.66" "10.1" "39.9" " 65"
California "West" "21198" "5114" "1.1" "71.71" "10.3" "62.6" " 20"
Colorado "West" " 2541" "4884" "0.7" "72.06" " 6.8" "63.9" "166"Area
Alabama " 50708"
Alaska "566432"
Arizona "113417"
Arkansas " 51945"
California "156361"
Colorado "103766"
可以用method看as、is都有哪些函数可以进行数据转换
> methods(is)[1] is.Alignment is.array is.atomic [4] is.Border is.call is.CellBlock [7] is.CellProtection is.CellStyle is.character
[10] is.complex is.data.frame is.DataFormat
[13] is.double is.element is.empty.model
[16] is.environment is.expression is.factor
[19] is.Fill is.finite is.Font
[22] is.function is.infinite is.integer
[25] is.language is.leaf is.list
[28] is.loaded is.logical is.matrix
[31] is.mts is.na is.na.data.frame
[34] is.na.numeric_version is.na.POSIXlt is.na<-
[37] is.na<-.default is.na<-.factor is.na<-.numeric_version
[40] is.name is.nan is.null
[43] is.numeric is.numeric.Date is.numeric.difftime
[46] is.numeric.POSIXt is.numeric_version is.object
[49] is.ordered is.package_version is.pairlist
[52] is.primitive is.qr is.R
[55] is.raster is.raw is.recursive
[58] is.relistable is.single is.stepfun
[61] is.symbol is.table is.ts
[64] is.tskernel is.unsorted is.vector
see '?methods' for accessing help and source code
Warning message:
In .S3methods(generic.function, class, envir) :function 'is' appears not to be S3 generic; found functions that look like S3 methods
unname()
unlist()