📜  如何从R中的数据框列表中提取某些列?

📅  最后修改于: 2022-05-13 01:54:31.958000             🧑  作者: Mango

如何从R中的数据框列表中提取某些列?

嵌套列表可能包含其他列表,或各种对象、数组、向量或数据框作为其组件。每个组件都映射到 R 编程语言中的唯一索引位置。任何元素都可以使用[[]]运算符轻松索引,该运算符用于引用列表的特定索引。随后, list_name[[ index ]]将获取存储在索引位置的组件。在这种情况下,此方法返回索引位置处的数据帧。

然后可以使用 df$col-name 或 df[[col-indx]] 捕获提取的数据帧的特定列,它以向量的形式获取提到的列。提到的列应该在边界内,否则会引发错误。默认情况下,索引从 1 开始。

句法:

示例 1:



R
# declaring a dataframe
data_frame1 = data.frame(col1 = letters[1:4], 
                         col2 = c(5:8) , col3 = TRUE)
print ("DataFrame1")
print (data_frame1)
  
# declaring a dataframe
data_frame2 = data.frame(col1 = c(6:9), 
                         col2 = LETTERS[1:4])
print ("DataFrame2")
print (data_frame2)
  
combined_df <- list(data_frame1,data_frame2)
print ("Dataframe2 Column1")
col21 <- combined_df[[2]][[1]]
print (col21)


R
# declaring a dataframe
data_frame1 = data.frame(col1 = letters[1:4], 
                         col2 = c(5:8) , col3 = TRUE)
print ("DataFrame1")
print (data_frame1)
  
# declaring a dataframe
data_frame2 = data.frame(col1 = c(6:9), 
                         col2 = LETTERS[1:4])
print ("DataFrame2")
print (data_frame2)
  
# declaring a dataframe
data_frame3 = data.frame(C1 = FALSE, C2 = 1)
print ("DataFrame3")
print (data_frame3)
  
combined_df <- list(data_frame1,data_frame2,data_frame3)
print ("Dataframe3 Column2")
col32 <- combined_df[[3]][[2]]
print (col32)


R
# declaring a dataframe
data_frame1 = data.frame(col1 = letters[1:4], 
                         col2 = c(5:8) , col3 = TRUE)
print ("DataFrame1")
print (data_frame1)
  
# declaring a dataframe
data_frame2 = data.frame(col1 = c(6:9), 
                         col2 = LETTERS[1:4])
print ("DataFrame2")
print (data_frame2)
  
# declaring a dataframe
data_frame3 = data.frame(C1 = FALSE, C2 = 1)
print ("DataFrame3")
print (data_frame3)
  
combined_df <- list(data_frame1,data_frame2,data_frame3)
print ("Dataframe1 Column1")
col11 <- combined_df[[1]]$col1
print (col11)


输出

[1] "DataFrame1"
 col1 col2 col3
1    a    5 TRUE
2    b    6 TRUE
3    c    7 TRUE
4    d    8 TRUE
[1] "DataFrame2"
 col1 col2
1    6    A
2    7    B
3    8    C
4    9    D
[1] "Dataframe2 Column1"
[1] 6 7 8 9

list() 方法可能包含两个以上可以堆叠在一起的组件。数据帧的列可以是单个元素或原子元素的向量。

示例 2:

电阻

# declaring a dataframe
data_frame1 = data.frame(col1 = letters[1:4], 
                         col2 = c(5:8) , col3 = TRUE)
print ("DataFrame1")
print (data_frame1)
  
# declaring a dataframe
data_frame2 = data.frame(col1 = c(6:9), 
                         col2 = LETTERS[1:4])
print ("DataFrame2")
print (data_frame2)
  
# declaring a dataframe
data_frame3 = data.frame(C1 = FALSE, C2 = 1)
print ("DataFrame3")
print (data_frame3)
  
combined_df <- list(data_frame1,data_frame2,data_frame3)
print ("Dataframe3 Column2")
col32 <- combined_df[[3]][[2]]
print (col32)

输出

[1] "DataFrame1"
 col1 col2 col3
1    a    5 TRUE
2    b    6 TRUE
3    c    7 TRUE
4    d    8 TRUE
[1] "DataFrame2"
 col1 col2
1    6    A
2    7    B
3    8    C
4    9    D
[1] "DataFrame3"
    C1 C2
1 FALSE  1
[1] "Dataframe3 Column2"
[1] 1

列名也可以使用 $运算符引用,它在 R 中用作索引运算符。

示例 3:

电阻

# declaring a dataframe
data_frame1 = data.frame(col1 = letters[1:4], 
                         col2 = c(5:8) , col3 = TRUE)
print ("DataFrame1")
print (data_frame1)
  
# declaring a dataframe
data_frame2 = data.frame(col1 = c(6:9), 
                         col2 = LETTERS[1:4])
print ("DataFrame2")
print (data_frame2)
  
# declaring a dataframe
data_frame3 = data.frame(C1 = FALSE, C2 = 1)
print ("DataFrame3")
print (data_frame3)
  
combined_df <- list(data_frame1,data_frame2,data_frame3)
print ("Dataframe1 Column1")
col11 <- combined_df[[1]]$col1
print (col11)

输出

[1] "DataFrame1"
 col1 col2 col3
1    a    5 TRUE
2    b    6 TRUE
3    c    7 TRUE
4    d    8 TRUE
[1] "DataFrame2"
 col1 col2
1    6    A
2    7    B
3    8    C
4    9    D
[1] "DataFrame3"
    C1 C2
1 FALSE  1
[1] "Dataframe1 Column1"
[1] a b c d
Levels: a b c d