開始使用免費開始

建立具名列表

做得好,你的進度很順利!

就像你的待辦清單一樣,你不希望忘記或不清楚列表中每個元件代表什麼。因此應該替它們命名:

my_list <- list(name1 = your_comp1, 
                name2 = your_comp2)

這會建立一個列表,其中的元件被命名為 name1name2,以此類推。若你想在建立列表之後再命名,也可以像對向量那樣使用 names() 函式。以下指令與上面的指定完全等價:

my_list <- list(your_comp1, your_comp2)
names(my_list) <- c("name1", "name2")

本練習屬於課程

R 入門

檢視課程

練習說明

  • 修改上一個練習的程式碼(見編輯器),為各元件加上名稱。my_vector 用名稱 vecmy_matrix 用名稱 matmy_df 用名稱 df
  • 列印 my_list,以便檢視輸出。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Vector with numerics from 1 up to 10
my_vector <- 1:10 

# Matrix with numerics from 1 up to 9
my_matrix <- matrix(1:9, ncol = 3)

# First 10 elements of the built-in data frame mtcars
my_df <- mtcars[1:10,]

# Adapt list() call to give the components names
my_list <- list(my_vector, my_matrix, my_df)

# Print out my_list
編輯並執行程式碼