In this article, we will discuss how a dataframe can be converted to a vector in R. For the Conversion of dataframe into a vector, we can simply pass the dataframe column name as [[index]].
Approach:
We are taking a column in the dataframe and passing it into another variable by the selection method. Selection method can be defined as choosing a column from a data frame using ” [[]]”.
- Create a dataframe
- Apply selection process on the columns in a dataframe which is created.
- Verify the resultant variable.
Syntax:
convert_data = dataframe_name[[‘column_name’]]
Given below are implementations using this approach to produce this functionality.
Example 1:
R
names= c ( 'sravan' , 'bobby' , 'ojaswi' , 'gnanesh' , 'rohith' , 'satwik' )
marks1= c (96,76,82,89,100,94)
marks2= c (98,79,98,78,98,89)
print (names)
print (marks1)
print (marks2)
print ( "----------------------" )
Final_data = data.frame (names,marks1,marks2)
print (Final_data)
print ( "-------------------conversion---------------------" )
convert_data1 = Final_data[[ 'marks1' ]]
print (convert_data1)
convert_data2 = Final_data[[ 'marks2' ]]
print (convert_data2)
|
Output:

Example 2:
R
subjects= c ( 'java' , 'python' , 'c/c++' )
teachers= c ( 'ravi' , 'swapna' , 'sai' )
pass= c (98.11,90.45,95)
print (subjects)
print (teachers)
print (pass)
print ( "----------------------" )
Final_data = data.frame (subjects,teachers,pass)
print (Final_data)
print ( "-------------------conversion---------------------" )
convert_data1 = Final_data[[ 'subjects' ]]
print (convert_data1)
convert_data2 = Final_data[[ 'teachers' ]]
print (convert_data2)
convert_data2 = Final_data[[ 'pass' ]]
print (convert_data2)
|
Output:
