Open In App

Extract Single Column as DataFrame in R

Last Updated : 16 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how a single column can be extracted as Dataframe using R programming language.

Database in use:

Data Frame GFG

Method 1: Extract Data from Data Frame using column name

In this method, a specific column can be extracted from the dataframe using its name using $.

Syntax:

dataframe$column_name

The column returned will be in a form of a vector, but it can be converted to a dataframe explicitly using data.frame() function of R language.

Example:

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)


Output 

Here, we can see that two columns are extracted Name & Salary

Method 2: Extract Data from Data Frame using square bracket

The column can also be extracted as a dataframe using square brackets with an index of the column in the dataframe given to it as input.

Example:

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]


Output

Display the 1st and 2nd column

Method 3: Extract Single Column as DataFrame 

 We extract columns as a vector from an R data frame, but sometimes we might need a column as a data frame. Thus, we can use as.data.frame to extract columns that we want to extract as a data frame with single square brackets. The purpose behind this could be merging the column with another data frame.

Example:

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) 


Output

Extracting 2nd column as a separate data frame

Extracting 3rd column as a separate data frame

Method 4: Extract Single Variable as Data Frame Using drop Argument

If the drop argument is provided with the value FALSE the columns are not converted to vector objects.

Example:

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]  


Output 

Displaying the 1st and 2nd column



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads