Extract Single Column as DataFrame in R

16 May, 2021
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
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 dataframespecific_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
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 Bracketsgfg.data[1]gfg.data[2]
Output
Display the 1st and 2nd column
 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 # dataframex2 <-as.data.frame(gfg.data[,2])print(x2)   # Extracting 3rd column as a separate # dataframex3 <-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
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 Argumentgfg.data[ , 1, drop = FALSE]  gfg.data[ , 2, drop = FALSE]  
Output 
Displaying the 1st and 2nd column
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.