📜  在 R 中提取单列作为 DataFrame

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

在 R 中提取单列作为 DataFrame

在本文中,我们将看到如何使用 R 编程语言将单个列提取为 Dataframe。

使用中的数据库:

数据帧 GFG

方法 1:使用列名从 Data Frame 中提取数据

在此方法中,可以使用 $ 使用其名称从数据框中提取特定列。

句法:

返回的列将采用向量的形式,但可以使用 R 语言的 data.frame()函数将其显式转换为数据帧。

例子:

R
# Create the data frame.
gfg.data <- data.frame(
  GFG_Id = c (1:7), 
    
  GFG_Name = c("Damon","Joe","Jenna","Ryan",
               "Bonnie","Stefan","William"),
    
  GFG_Sal = c(6200,5152,6110,7290,8485,7654,
              2341), 
    
  Start_date = as.Date(c("2021-01-01", "2016-06-27", 
                         "2014-1-09", "2017-02-14",
                         "2009-03-26","2019-06-27",
                         "2020-09-27")),
    
  stringsAsFactors = FALSE)
  
# Print the data frame.            
print(gfg.data)
  
# Extract Specific columns
# Convert it explicitly to a dataframe
specific_col <- data.frame(gfg.data$ GFG_Name,
                           gfg.data$ GFG_Sal)
print(specific_col)


R
# Create the data frame.
gfg.data <- data.frame(
  GFG_Id = c (1:7), 
    
  GFG_Name = c("Damon","Joe","Jenna","Ryan",
               "Bonnie","Stefan","William"),
    
  GFG_Sal = c(6200,5152,6110,7290,8485,7654,
              2341), 
    
  Start_date = as.Date(c("2021-01-01", "2016-06-27", 
                         "2014-1-09", "2017-02-14",
                         "2009-03-26","2019-06-27",
                         "2020-09-27")),
    
  stringsAsFactors = FALSE)
  
# Print the data frame.            
print(gfg.data)
  
# Extract Single Variable as Data Frame
# Using Square Brackets
gfg.data[1]
gfg.data[2]


R
# Create the data frame.
gfg.data <- data.frame(
  GFG_Id = c (1:7), 
    
  GFG_Name = c("Damon","Joe","Jenna","Ryan",
               "Bonnie","Stefan","William"),
    
  GFG_Sal = c(6200,5152,6110,7290,8485,7654,
              2341), 
    
  Start_date = as.Date(c("2021-01-01", "2016-06-27", 
                         "2014-1-09", "2017-02-14",
                         "2009-03-26","2019-06-27",
                         "2020-09-27")),
  stringsAsFactors = FALSE)
  
# Print the data frame.            
print(gfg.data)
  
# Extracting 2nd column as a separate 
# dataframe
x2 <-as.data.frame(gfg.data[,2])
print(x2) 
  
# Extracting 3rd column as a separate 
# dataframe
x3 <-as.data.frame(gfg.data[,3])
print(x3)


R
# Create the data frame.
gfg.data <- data.frame(
  GFG_Id = c (1:7), 
    
  GFG_Name = c("Damon","Joe","Jenna","Ryan",
               "Bonnie","Stefan","William"),
    
  GFG_Sal = c(6200,5152,6110,7290,8485,7654,
              2341), 
    
  Start_date = as.Date(c("2021-01-01", "2016-06-27",
                         "2014-1-09", "2017-02-14",
                         "2009-03-26","2019-06-27",
                         "2020-09-27")),
    
  stringsAsFactors = FALSE)
  
# Print the data frame.            
print(gfg.data)
  
# Extract Single Variable as Data Frame 
# Using drop Argument
gfg.data[ , 1, drop = FALSE]  
gfg.data[ , 2, drop = FALSE]


输出

在这里,我们可以看到提取了两列 Name & Salary

方法二:使用方括号从数据框中提取数据

也可以使用方括号将该列提取为数据帧,其中将数据帧中列的索引作为输入。

例子:

电阻

# Create the data frame.
gfg.data <- data.frame(
  GFG_Id = c (1:7), 
    
  GFG_Name = c("Damon","Joe","Jenna","Ryan",
               "Bonnie","Stefan","William"),
    
  GFG_Sal = c(6200,5152,6110,7290,8485,7654,
              2341), 
    
  Start_date = as.Date(c("2021-01-01", "2016-06-27", 
                         "2014-1-09", "2017-02-14",
                         "2009-03-26","2019-06-27",
                         "2020-09-27")),
    
  stringsAsFactors = FALSE)
  
# Print the data frame.            
print(gfg.data)
  
# Extract Single Variable as Data Frame
# Using Square Brackets
gfg.data[1]
gfg.data[2]

输出



显示第一列和第二列

方法 3:提取单列作为 DataFrame

 我们从 R 数据框中提取列作为向量,但有时我们可能需要一列作为数据框。因此,我们可以使用as.data.frame来提取我们想要提取为带有单个方括号的数据框的列。这背后的目的可能是将该列与另一个数据框合并。

例子:

电阻

# Create the data frame.
gfg.data <- data.frame(
  GFG_Id = c (1:7), 
    
  GFG_Name = c("Damon","Joe","Jenna","Ryan",
               "Bonnie","Stefan","William"),
    
  GFG_Sal = c(6200,5152,6110,7290,8485,7654,
              2341), 
    
  Start_date = as.Date(c("2021-01-01", "2016-06-27", 
                         "2014-1-09", "2017-02-14",
                         "2009-03-26","2019-06-27",
                         "2020-09-27")),
  stringsAsFactors = FALSE)
  
# Print the data frame.            
print(gfg.data)
  
# Extracting 2nd column as a separate 
# dataframe
x2 <-as.data.frame(gfg.data[,2])
print(x2) 
  
# Extracting 3rd column as a separate 
# dataframe
x3 <-as.data.frame(gfg.data[,3])
print(x3) 

输出

将第二列提取为单独的数据框

将第三列提取为单独的数据框

方法 4:使用 drop Argument 提取单个变量作为数据帧

如果drop 参数的值为 FALSE,则列不会转换为向量对象。

例子:

电阻

# Create the data frame.
gfg.data <- data.frame(
  GFG_Id = c (1:7), 
    
  GFG_Name = c("Damon","Joe","Jenna","Ryan",
               "Bonnie","Stefan","William"),
    
  GFG_Sal = c(6200,5152,6110,7290,8485,7654,
              2341), 
    
  Start_date = as.Date(c("2021-01-01", "2016-06-27",
                         "2014-1-09", "2017-02-14",
                         "2009-03-26","2019-06-27",
                         "2020-09-27")),
    
  stringsAsFactors = FALSE)
  
# Print the data frame.            
print(gfg.data)
  
# Extract Single Variable as Data Frame 
# Using drop Argument
gfg.data[ , 1, drop = FALSE]  
gfg.data[ , 2, drop = FALSE]  

输出

显示第一列和第二列