Wednesday 17 June 2015

R : Lists

List is a vector, can contain different types of objects.
> list1 <- list(1, 2, "Hello", TRUE, 10+5i)
> list1
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] "Hello"

[[4]]
[1] TRUE

[[5]]
[1] 10+5i

> list2 <- list(1, 2, "Hello", TRUE, 10+5i, list(2, "3", "aaa"))
> list2
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] "Hello"

[[4]]
[1] TRUE

[[5]]
[1] 10+5i

[[6]]
[[6]][[1]]
[1] 2

[[6]][[2]]
[1] "3"

[[6]][[3]]
[1] "aaa"

Accessing elements of list

You can access elements of list by using indexes.

> list2[1]
[[1]]
[1] 1

> 
> list2[6]
[[1]]
[[1]][[1]]
[1] 2

[[1]][[2]]
[1] "3"

[[1]][[3]]
[1] "aaa"



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment