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
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 (gfg.data)
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
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 (gfg.data)
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
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 (gfg.data)
x2 <- as.data.frame (gfg.data[,2])
print (x2)
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
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 (gfg.data)
gfg.data[ , 1, drop = FALSE ]
gfg.data[ , 2, drop = FALSE ]
|
Output

Displaying the 1st and 2nd column
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
16 May, 2021
Like Article
Save Article