Extract specific column from a DataFrame using column name in R
Last Updated :
15 Feb, 2022
In this article, we are going to see how to extract a specific column from a dataframe using the column name in R Programming Language.
In the data.frame() we have to pass dataframe_name followed by $ symbol followed by column name. The reason to pass dataframe_name$ column name to data.frame() is, after extracting the data from column we have to show the data in the rows and column format. So we pass dataframe_name $ column name to the data.frame().
Syntax of $ operator
data.frame ( dataframe_name $ column_name )
Example 1: In this example, we just created a data frame using data.frame() function and passed 3 vectors which holds some values. In the second step, we used $ operator along with the data frame name inside the data.frame(). The reason behind passing dataframe_name $ column name into data.frame() is to show the extracted column in data frame format.
R
df1= data.frame (number = c (1:3),
string = c ( "One" , "Two" , "Three" ) ,
Binary = c (001,010,011))
data.frame (df1$number)
|
Output:
Example 2: In this example, we have created 2 vectors named ranking and name with some data inside. Passed the 2 vectors into the data.frame() function as parameters and assigned it to a variable called df1, finally using $ operator we are extracting the name column and passing it to data.frame() function for showing in dataframe format.
R
ranking = c (1 : 3)
name = c ( "Mani sharma" ,
"Devi sri prasad" ,
"Thaman SS" )
df1 = data.frame (ranking,name)
data.frame (df1$name)
|
Output:
Example 3: In this example, we have created 2 vectors named ranking and name with some data inside. Passing the 2 vectors into the data.frame() function as parameters. Assigned the data.frame() function into a variable named df1. Using $ operator along with dataframe_name to extract column name and passed it into data.frame() function to show the extracted column name in data frame format.
R
ranking = c (1 : 3)
name = c ( "Trivikram" , "RajaMouli SS" , "Puri Jagannadh" )
df1 = data.frame (ranking,name)
data.frame (df1$name)
|
Output:
Extracting Multiple columns from dataframe
Multiple column extraction can be done through indexing.
Syntax : variable_name = dataframe_name [ row(s) , column(s) ]
Example 1: a=df[ c(1,2) , c(1,2) ]
Explanation : if we want to extract multiple rows and columns we can use c() with row names and column names as parameters. Here in the above example we have extracted 1,2 rows and 1,2 columns data from a data frame and stored into a variable.
Example 2 : b=df [ c(1,2) , c(“id”,”name”) ]
Explanation : If we want to specify column names we can give column names as parameters in c() function . In the above example we have extracted 1,2 rows of ID and name columns.
Example 1: First, we are creating a data frame with some data. Using indexing we are extracting multiple columns. In the above example, we have extracted all rows and 2 columns named number and string from df1 and storing into another variable. Finally, printing the df2.
R
df1= data.frame (number= c (1:3),
string= c ( "One" , "Two" , "Three" ) ,
Binary= c (001,010,011))
df2 = df1[ c (1:3), c ( "string" , "Binary" )]
print (df2)
|
Output:
Example 2: First, we are creating a data frame with some data. Using indexing we are extracting multiple columns. In the above example, we have extracted 1,2 rows and 2 columns named ranking and name from df1 and storing them into another variable. Finally, printing the df2.
R
ranking = c (1 : 3)
name = c ( "Trivikram" , "RajaMouli SS" , "Puri Jagannadh" )
no_of_movies = c (15, 10, 12)
df1 = data.frame (ranking,name,no_of_movies)
df2 = df1[1:2 , c ( "ranking" , "name" )]
print (df2)
|
Output:
Example 3: First we are creating a data frame with some data. Using indexing we are extracting multiple columns. In the above example, we have extracted all rows and 2 columns named name and no_of_movies from df1 and storing into another variable. Finally, printing the df2.
R
ranking = c (1 : 3)
name = c ( "Mani sharma" , "Devi sri prasad" , "Thaman SS" )
no_of_movies= c (20, 30, 40)
df1 = data.frame (ranking,name,no_of_movies)
df2 = df1[, c ( "name" , "no_of_movies" )]
print (df2)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...