R語言資料框 因子 列表
阿新 • • 發佈:2019-01-03
資料框:
data <- data.frame(col1,col2,)
資料框
patientID <- c(1,2,3,4)
age <- c(25,34,28,52)
diabetes <- c("Type1","Type2","Type3","Type2")
status <- c("poor","improved","Excellent","poor")
patientsData <- data.frame(patientID,age,diabetes,status)
//取出該列下的資料
patientsData[1:2]
patientsData[c("diabetes","status")]
patientsData$age :取出資料集中age的數值
head(mtcars)
mtcars$mpg
attach(mtcars):將mtcars的資料集新增到R的搜尋路徑中
mpg
detach(mtcars):將mecars從搜尋路徑中移除,並沒有將mtcars資料框移除
with(mtcars,{l<-mpg l })
因子:
diabetes <- factor(diabetes)
列表
mylist <- list(object1,object2,....)
g <- "My first list"
h <- c(12,45,43,90)
j <- matrix(1:10,nrow=2)
k <- c("one","two","three")
mylist <- list(g,h,j,k)
mylist[[2]]
mylist <- list(stud.id=1234,stud.name="Tom",stud.marks=C(12,3,14,25,19))
mylist[[1]]
mylist
mode(mylist[[1]]):求出型別
mode(mylist[1])
mylist$stud.id
names(mylist)
names(mylist) <- c("id","name","marks")
mylist$parents <- c("Mama","Joe"):向列表中新增資料集
length(mylist)
mylist <- mylist[-4]: 刪除第四個成分
other <- list(age=19,sex="male")
lst <- c(mylist,other):將兩個列表進行合併
unlist() : 將列表中的元素轉化為向量的形式
unlist(lst)
data <- data.frame(col1,col2,)
資料框
patientID <- c(1,2,3,4)
age <- c(25,34,28,52)
diabetes <- c("Type1","Type2","Type3","Type2")
status <- c("poor","improved","Excellent","poor")
patientsData <- data.frame(patientID,age,diabetes,status)
//取出該列下的資料
patientsData[1:2]
patientsData[c("diabetes","status")]
patientsData$age :取出資料集中age的數值
head(mtcars)
mtcars$mpg
attach(mtcars):將mtcars的資料集新增到R的搜尋路徑中
mpg
detach(mtcars):將mecars從搜尋路徑中移除,並沒有將mtcars資料框移除
with(mtcars,{l<-mpg l })
因子:
diabetes <- factor(diabetes)
列表
mylist <- list(object1,object2,....)
g <- "My first list"
h <- c(12,45,43,90)
j <- matrix(1:10,nrow=2)
k <- c("one","two","three")
mylist <- list(g,h,j,k)
mylist[[2]]
mylist <- list(stud.id=1234,stud.name="Tom",stud.marks=C(12,3,14,25,19))
mylist[[1]]
mylist
mode(mylist[[1]]):求出型別
mode(mylist[1])
mylist$stud.id
names(mylist)
names(mylist) <- c("id","name","marks")
mylist$parents <- c("Mama","Joe"):向列表中新增資料集
length(mylist)
mylist <- mylist[-4]: 刪除第四個成分
other <- list(age=19,sex="male")
lst <- c(mylist,other):將兩個列表進行合併
unlist() : 將列表中的元素轉化為向量的形式
unlist(lst)