R'de boş liste nasıl oluşturulur (örneklerle)


R’de boş bir liste oluşturmak için aşağıdaki sözdizimini kullanabilirsiniz:

 #create empty list with length of zero
empty_list <- list()

#create empty list of length 10
empty_list <- vector(mode=' list ', length= 10 )

Aşağıdaki örnekler bu fonksiyonların pratikte nasıl kullanılacağını göstermektedir.

Örnek 1: R’de sıfır uzunlukta boş bir liste oluşturun

Aşağıdaki kod, R’de sıfır uzunlukta boş bir listenin nasıl oluşturulacağını gösterir:

 #create empty list
empty_list <- list()

#verify that empty_list is of class 'list'
class(empty_list)

[1] "list"

#view length of list
length(empty_list)

[1] 0

Sonuç, uzunluğu 0 olan bir listedir.

Örnek 2: R’de belirli uzunlukta boş bir liste oluşturun

Aşağıdaki kod, R’de uzunluğu 8 olan boş bir listenin nasıl oluşturulacağını gösterir:

 #create empty list of length 8
empty_list <- vector(mode=' list ', length= 8 )

#verify that empty_list is of class 'list'
class(empty_list)

[1] "list"

#view list
empty_list
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

[[5]]
NULL

[[6]]
NULL

[[7]]
NULL

[[8]]
NULL

Sonuç, listedeki her öğenin NULL olduğu 8 uzunluğunda bir listedir.

Örnek 3: R’de Boş Bir Listeye Değer Ekleme

Boş bir liste oluşturmanın en yaygın nedenlerinden biri, onu bir döngü kullanarak değerlerle doldurmaktır.

Aşağıdaki kod, boş bir listenin nasıl oluşturulacağını ve ardından bu listenin değerlerle nasıl doldurulacağını gösterir:

 #create empty list of length 8
empty_list <- vector(mode=' list ', length= 8 )

#get length of list
len <- length(empty_list)

#define values to append to list
new <- c(3, 5, 12, 14, 17, 18, 18, 20)

#fill values in list
i = 1
while (i <= length(new)) {
    empty_list[[i]] <- new[i]
    i <- i + 1
}

#display updated list
empty_list

[[1]]
[1] 3

[[2]]
[1] 5

[[3]]
[1] 12

[[4]]
[1] 14

[[5]]
[1] 17

[[6]]
[1] 18

[[7]]
[1] 18

[[8]]
[1] 20

Boş listenin artık belirttiğimiz yeni değerlerle doldurulduğuna dikkat edin.

Ek kaynaklar

R’de boş bir veri çerçevesi nasıl oluşturulur
R’deki listeye değerler nasıl eklenir?
Bir liste R’de bir vektöre nasıl dönüştürülür?

Yorum ekle

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir