The Wayback Machine - https://web.archive.org/web/20240713070030/https://www.geeksforgeeks.org/convert-dataframe-to-vector-in-r/
Open In App

Convert DataFrame to vector in R

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

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




# creating a vector for names
names=c('sravan','bobby','ojaswi','gnanesh','rohith','satwik')
  
# creating vector for marks 1
marks1=c(96,76,82,89,100,94)
  
# creating vector for marks 2
marks2=c(98,79,98,78,98,89)
  
# display all details
print(names)
print(marks1)
print(marks2)
print("----------------------")
  
# passing vectors to the data frame
Final_data = data.frame(names,marks1,marks2)
  
# display data frame
print(Final_data)
print("-------------------conversion---------------------")
  
# converting marks1 dataframe column into vector
# by passing as index
convert_data1 = Final_data[['marks1']]
print(convert_data1)
  
# converting marks2 dataframe column into vector 
# by passing as index
convert_data2 = Final_data[['marks2']]
print(convert_data2)


Output:

Example 2:

R




# creating a vector for subjects
subjects=c('java','python','c/c++')
  
# creating vector for teachers
teachers=c('ravi','swapna','sai')
  
# creating vector for pass percentage
pass=c(98.11,90.45,95)
  
# display all details
print(subjects)
print(teachers)
print(pass)
print("----------------------")
  
# passing vectors to the data frame
Final_data = data.frame(subjects,teachers,pass)
  
# display data frame
print(Final_data)
print("-------------------conversion---------------------")
  
# converting subjects dataframe column into 
# vector by passing as index
convert_data1 = Final_data[['subjects']]
print(convert_data1)
  
# converting teachers dataframe column into 
# vector by passing as index
convert_data2 = Final_data[['teachers']]
print(convert_data2)
  
# converting pass dataframe column into vector
# by passing as index
convert_data2 = Final_data[['pass']]
print(convert_data2)


Output:



Similar Reads

Convert an Object into a Vector in R Programming - as.vector() Function
as.vector() function in R Language is used to convert an object into a vector. Syntax: as.vector(x) Parameters: x: Object to be converted Example 1: # R program to convert an object to vector # Creating an array x <- array(c(2, 3, 4, 7, 2, 5), c(3, 2)) x # Calling as.vector() Function as.vector(x) Output: [, 1] [, 2] [1, ] 2 7 [2, ] 3 2 [3, ] 4
1 min read
Check for the Existence of a Vector Object in R Programming - is.vector() Function
is.vector() function in R Language is used to return TRUE if the given vector is of the specified mode having no attributes other than names. It returns FALSE otherwise. Syntax: is.vector(x, mode = "any") Parameters: x: R object mode: character string naming an atomic mode or "list" or "expression" or "any" Example 1: # R program to illustrate # is
1 min read
Create a Vector of Colors from a Vector of Gray Levels in R Programming - gray() or grey() Function
gray() or grey() function in R Language is used to create a vector of colors from a vector of gray levels. Syntax: gray(level) or grey(level) Parameters: level: a vector of desired gray levels between 0 and 1; zero indicates "black" and one indicates "white". Example 1: # R program to illustrate # gray function # Calling the gray() function gray(0)
1 min read
How to create a new vector from a given vector in R
In this article, we will discuss How to create a new vector from a given vector in R Programming Language. Create a new vector from a given vectorYou can use various functions and techniques depending on your needs to create a new vector from a given vector in R. Here are some common methods. 1. Subset the VectorIf you want to create a new vector w
2 min read
How to Split Vector and DataFrame in R
R is a programming language and environment specifically designed for facts analysis, statistical computing, and graphics. Sometimes it is required to split data into batches for various data manipulation and analysis tasks. In this article, we will discuss some techniques to split vectors into chunks using the R Programming Language. Concepts rela
6 min read
R - Create Dataframe From Existing Dataframe
Create Dataframes when dealing with organized data so sometimes we also need to make Dataframes from already existing Dataframes. In this Article, let's explore various ways to create a data frame from an existing data frame in R Programming Language. Ways to Create Dataframe from Existing Dataframe Using Base R data. frame() FunctionUsing subset()
6 min read
Convert elements of a Vector to Strings in R Language - toString() Function
toString() function in R Programming Language is used to produce a single character string describing an R object. Syntax: toString(x, width = NULL) Parameters: x: R objectwidth: Suggestion for the maximum field width. Values of NULL or 0 indicate no maximum. The minimum value accepted is 6 and smaller values are taken as 6 toString() function in R
2 min read
Convert a Vector into Factor in R Programming - as.factor() Function
as.factor() function in R Programming Language is used to convert the passed object(usually Vector) into a Factor. Syntax: as.factor(object) Parameters: Object: Vector to be convertedas.factor() Function in R ExampleExample 1: Convert a Factor in R C/C++ Code # Creating a vector x<-c("female", "male", "male", "
1 min read
Convert values of an Object to Logical Vector in R Programming - as.logical() Function
as.logical() function in R Language is used to convert an object to a logical vector. Syntax: as.logical(x)Parameters: x: Numeric or character object R - as.logical() Function ExampleExample 1: Basic example of as.logical() Function in R Programming Language. C/C++ Code # R Program to convert # an object to logical vector # Creating a vector x <
1 min read
Convert a vector into a diagonal matrix in R
In this article, we will examine various methods to convert a vector into a diagonal matrix by using R Programming Language. What is a diagonal matrix?A diagonal matrix is a special type of square matrix where all elements, except those on the main diagonal (running from the top-left to the bottom-right), are zeros. This diagonal matrix can contain
3 min read
Article Tags :
three90RightbarBannerImg