R'de listeler nasıl alt kümelenir (örneklerle)
R’deki listeleri alt kümelemek için aşağıdaki sözdizimini kullanabilirsiniz:
#extract first list item my_list[[1]] #extract first and third list item my_list[c(1, 3)] #extract third element from the first item my_list[[c(1, 3)]]
Aşağıdaki örnekler, bu sözdiziminin aşağıdaki listeyle nasıl uygulanacağını gösterir:
#create list my_list <- list(a = 1:3, b = 7, c = " hey ") #view list my_list $a [1] 1 2 3 $b [1] 7 $c [1] “hey”
Örnek 1: Bir liste öğesini çıkarma
Aşağıdaki kod, bir liste öğesini çıkarmanın farklı yollarını gösterir:
#extract first list item using index value my_list[[1]] [1] 1 2 3 #extract first list item using name my_list[[" a "]] [1] 1 2 3 #extract first list item using name with $ operator my_list$a [1] 1 2 3
Her üç yöntemin de aynı sonuca yol açtığını unutmayın.
Örnek 2: Birden çok liste öğesini ayıklama
Aşağıdaki kod, birden çok liste öğesini çıkarmanın farklı yollarını gösterir:
#extract first and third list item using index values my_list[c(1, 3)] $a [1] 1 2 3 $c [1] “hey” #extract first and third list item using names my_list[c(" a ", " c ")] $a [1] 1 2 3 $c [1] "hey"
Her iki yöntem de aynı sonuca götürür.
Örnek 3: Belirli bir öğeyi liste öğesinden çıkarma
Aşağıdaki kod, bir liste öğesinden belirli bir öğeyi çıkarmanın farklı yollarını gösterir:
#extract third element from the first item using index values my_list[[c(1, 3)]] [1] 3 #extract third element from the first item using double brackets my_list[[1]][[3]] [1] 3
Her iki yöntem de aynı sonuca götürür.
Ek kaynaklar
Bir listeyi R’de veri çerçevesine dönüştürme
R’deki listeye değerler nasıl eklenir?