R
Let's create a list containing numeric, character and vector data types:
> x <- list(batch=3,label="Lung Cancer Patients", subtype=c("A","B","C")) > x
$batch [1] 3 $label [1] "Lung Cancer Patients" $subtype [1] "A" "B" "C"
> typeof(x)[1] "list" > length(x)[1] 3 > is.list(x)
[1] TRUE
> str(x)List of 3 $ batch : num 3 $ label : chr "Lung Cancer Patients" $ subtype: chr [1:3] "A" "B" "C"
The elements of
> x[[1]]
[1] 3
x[[2]]
[1] "Lung Cancer Patients"
x[[3]]
[1] "A" "B" "C"
x[[3]][2]
[1] "B"
The elements of
> x$subtype
[1] "A" "B" "C"
> x[["subtype"]]
[1] "A" "B" "C"
Use
> names(x)
[1] "batch" "label" "subtype"
The statement
> length(x)
[1] 3
To delete an element of list, just assign its value to NULL:
> x$batch <- NULL > x$label [1] "Lung Cancer Patients" $subtype [1] "A" "B" "C"
Function
> y <- list(operator="Mary",location="New York") > z <- list(cost=1000.24,urgent="yes") > final_list <- c(x,y,z) > final_list
$batch [1] 3 $label [1] "Lung Cancer Patients" $subtype [1] "A" "B" "C" $operator [1] "Mary" $location [1] "New York" $cost [1] 1000.24 $urgent [1] "yes"
> BODTime demand 1 1 8.3 2 2 10.3 3 3 19.0 4 4 16.0 5 5 15.6 6 7 19.8 > as.list(BOD)$Time [1] 1 2 3 4 5 7 $demand [1] 8.3 10.3 19.0 16.0 15.6 19.8 attr(,"reference") [1] "A1.4, p. 270" > x <- factor(c(1,3,5,1,3,5,3)) > as.list(x)[[1]] [1] 1 Levels: 1 3 5 [[2]] [1] 3 Levels: 1 3 5 [[3]] [1] 5 Levels: 1 3 5 [[4]] [1] 1 Levels: 1 3 5 [[5]] [1] 3 Levels: 1 3 5 [[6]] [1] 5 Levels: 1 3 5 [[7]] [1] 3 Levels: 1 3 5
List to data frame:
> y <- as.data.frame(x) > y
batch label subtype 1 3 Lung Cancer Patients A 2 3 Lung Cancer Patients B 3 3 Lung Cancer Patients C
List to matrix:
> y <- as.matrix(x) > y
[,1] batch 3 label "Lung Cancer Patients" subtype Character,3