List in R

Published by onesixx on

http://www.r-tutor.com/r-introduction/list
http://dsmoon.tistory.com/entry/R언어4리스트

List 란?

  • 다른 objects를 포함하는 generic vector

remove element of a List

LL <- list( "sixx", NULL, NULL, c(21,32,11), "bar")
[[1]]
[1] "sixx"

[[2]]
NULL

[[3]]
NULL

[[4]]
[1] 21 32 11

[[5]]
[1] "bar"
names(LL) <- seq_along(LL)
$`1`
[1] "sixx"

$`2`
NULL

$`3`
NULL

$`4`
[1] 21 32 11

$`5`
[1] "bar"

Remove by name

LL[["5"]] <- NULL

Remove by condition

LL[LL %in% "sixx"] <- NULL

Remove duplicated element

LL[duplicated(LL)]<-NULL

Remove NULL element

LL <- Filter(Negate(is.null), LL)
#or
LL[sapply(LL, print)] <- NULL
#or
LL[lengths(LL)!=0]

example2

https://statisticsglobe.com/remove-element-from-list-in-r
myList <- list(
            a = c(0, 8, 1, 9),                 
            b = 666,
            c = "sixx")

#1. assign NULL
myList[2] <- NULL
myList[c(2,3)] <- NULL
myList['c'] <- NULL

#2. minus sign
result <- myList[-2]
result <- myList[-c(2,3)]

#3. by Name 
result <- myList[names(myList) %in% "b"==FALSE]
result <- myList[names(myList) %in% c("b","c")==FALSE]

result <- myList[names(myList) !="b"]
col <- c('a','b')
for(i in col){
  i <- as.character(i)
  myList[i] <- NULL
}

List 생성및 Member Reference

Vector

pre  <- c(54, 64, 75)
post <- c(53, 60, 70, 60)
diet_named <- list(Before=pre, After=post)    

> diet_named
$Before
[1] 54 64 75

$After
[1] 53 60 70 60

> diet_named[[1]]
> diet_named$Before       # same 이름이 있는 경우에만 
[1] 54 64 75
diet_unnamed <- list(pre, post)
> diet_unnamed
[[1]]
[1] 54 64 75

[[2]]
[1] 53 60 70 60 

> diet_unnamed[[1]]
[1] 54 64 75
> diet_unnamed$Before       # 활용불가!! 
NULL

DataFrame

predd  <- data.table( height=c(54, 64, 75), weight=c(49, 52, 77))
postdd <- data.table( height=c(53, 60, 70), weight=c(49, 53, 70))
diet_DD <- list(Before=predd, After=postdd)

> predd         %>% class     # element is Data.Table
[1] "data.table" "data.frame"
> diet_DD[[1]]   %>% class
[1] "data.table" "data.frame"

unlist (Flatten Lists)

use.names = TRUE 인 경우, list명 + column명 + row 명 순

> diet_named %>% unlist()
Before1 Before2 Before3  After1  After2  After3  After4 
     54      64      75      53      60      70      60 

> diet_unnamed %>% unlist()
[1] 54 64 75 53 60 70 60

> diet_DD %>% unlist()                  # length 12인 numeric Vector
Before.height1 Before.height2 Before.height3 Before.weight1 Before.weight2 Before.weight3  After.height1  After.height2  After.height3  After.weight1 
            54             64             75             49             52             77             53             60             70             49 
 After.weight2  After.weight3 
            53             70 

recursive=F 인 경우, Flatten가능한 부분까지 ...
recursive=F 인 경우, 가장 상위 List만 Flatten 시킨다.

test <- list(Before=pre, After=postdd)

$Before
[1] 54 64 75

$After
   height weight
1:     53     49
2:     60     53
3:     70     70

> test %>% unlist()
> test %>% unlist(recursive=F)%>% unlist(recursive=F)   #same
      Before1       Before2       Before3 After.height1 After.height2 After.height3 After.weight1 After.weight2 After.weight3 
           54            64            75            53            60            70            49            53            70 

> test %>% unlist(recursive=F)
$Before1
[1] 54

$Before2
[1] 64

$Before3
[1] 75

$After.height
[1] 53 60 70

$After.weight
[1] 49 53 70

nested list

ui = fluidPage(
  selectizeInput("state", "Choose a state:", choices="", multiple=F),
  textOutput("result")
)
server = function(input, output, session) {
  # choiceList <- list( `2019-01-01` = list("2019-01-01-10.fst", "2019-01-01-2.fst", "2019-01-01-11.fst"),
  #                     `2019-02-01` = list("2019-02-01-12.fst", "2019-02-01-all.fst"),
  #                     `2019-03-01` = list("2019-03-01-3.fst",  "2019-03-01-20.fst", "2019-03-01-10.fst"))
  
  files <- c("2019-01-01-10.fst", "2019-01-01-2.fst", "2019-01-01-11.fst",
             "2019-02-01-12.fst", "2019-02-01-all.fst",
             "2019-03-01-3.fst",  "2019-03-01-20.fst", "2019-03-01-10.fst")
  groups <- str_sub(files, start=1, end=10) %>% unique()
  
  choiceList <- vector("list", length(groups))
  names(choiceList) <- groups
  for(i in seq_along(groups)){
    choiceList[[i]] <- files[str_detect(files, groups[i])] %>%
      str_replace_all(".fst","") %>% 
      str_split(" ")
  }
  updateSelectizeInput(session, inputId="state", choices=choiceList)
  
  output$result <- renderText({
    paste("You chose", input$state)
  })
}
shinyApp(ui, server)

vector vs. unvector(?)

> lc <- list(c("A","B","C"))

> lc
[[1]]
[1] "A" "B" "C"

----------------------------------------
lc <- c("A","B","C") %>% str_split(" ")
identical(l, lc)
----------------------------------------


> lc[[1]]
[1] "A" "B" "C"
> lc[[2]]
Error in lc[[2]] : subscript out of bounds


> lc[1]
[[1]]
[1] "A" "B" "C"

> lc[2]
[[1]]
NULL
> l  <- list("A","B","C")

> l
[[1]]
[1] "A"

[[2]]
[1] "B"

[[3]]
[1] "C"

> l[[1]]
[1] "A"
> l[[2]]
[1] "B"


> l[1]
[[1]]
[1] "A"

> l[2]
[[1]]
[1] "B"

Convert List to DT

#by row & column
do.call(cbind, myList))   %>% as.data.table()
do.call(cbind, myList))   %>% as.data.table()

Categories: Reshaping

onesixx

Blog Owner