Convert dataframe rows and columns to vector in R Last Updated : 05 Apr, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we are going to convert a dataframe column to a vector and a dataframe row to a vector in the R Programming Language. Convert dataframe columns into vectors We are taking a column in the data frame and passing it into another variable by using the selection method. The selection method can be defined as choosing a column from a data frame using the ” [[]]” operator. Steps - Create data frameSelect column to be convertedAssign it to a variableDisplay data frame so generated. Syntax: dataframe[[‘column’]] Example: R # create vectors id=c(7058,7084,7098) name=c('sravan','karthik','nikhil') # passing into dataframe data=data.frame(id,name) print(data) # convert id column into a vector column_data=data[['id']] print(column_data) # convert name column into a vector column_data1=data[['name']] print(column_data1) Output: Convert data frame row to a vector We can convert every row or entire dataframe by using a method called as.vector() Approach Create dataframeSelect row to be convertedPass it to the functionDisplay result Syntax: as.vector(t(dataframe_name)) Where t is the transpose of the dataframe. If t is not specified, the output is both rows and column names. If it is specified output is only rows. Example: without t specification. R # create vectors id=c(7058,7084,7098) name=c('sravan','karthik','nikhil') # passing into dataframe data=data.frame(id,name) print(data) print("-----------") # converting 1 st row to a vector as.vector((data[1,])) print("-----------") # converting 2nd row to a vector as.vector((data[2,])) print("-----------") # converting 3 rd row to a vector as.vector((data[3,])) print("-----------") Output: Example: Using t R # create vectors id=c(7058,7084,7098) name=c('sravan','karthik','nikhil') # passing into dataframe data=data.frame(id,name) print(data) print("-----------") # converting 1 st row to a vector as.vector(t(data[1,])) print("-----------") # converting 2nd row to a vector as.vector(t(data[2,])) print("-----------") # converting 3 rd row to a vector as.vector(t(data[3,])) print("-----------") Output: Display the entire dataframe as vectors Example: R # create vectors id=c(7058,7084,7098) name=c('sravan','karthik','nikhil') # passing into dataframe data=data.frame(id,name) print(data) print("-----------") # converting all dataframe to a vector as.vector(t(data)) Output: Example 2 : R # create vectors id=c(7058,7084,7098) address=c('guntur','hyd','kothapeta') name=c('sravan','karthik','nikhil') # passing into dataframe data=data.frame(id,address,name) print(data) print("-----dataframe row to a vector------") # converting dataframe to a vector as.vector(t(data)) print("-----dataframe column to a vector------") # converting dataframe 1 st column to a vector data1=data[['id']] print(data1) # converting dataframe 2 nd column to a vector data1=data[['address']] print(data1) # converting dataframe 3 rd column to a vector data1=data[['name']] print(data1) Output: Comment More infoAdvertise with us Next Article Convert dataframe rows and columns to vector in R S sravankumar_171fa07058 Follow Improve Article Tags : R Language R Programs R-DataFrame R DataFrame-Programs Similar Reads Convert dataframe column to list in R In this article, we will learn how to convert a dataframe into a list by columns in R Programming language. We will be using as.list() function, this function is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and data frames. Syntax: as.list( object ) Parameter 2 min read Convert Named Vector to DataFrame in R In this article, we will see how to convert the named vector to Dataframe in the R Programming Language. Method 1: Generally while converting a named vector to a dataframe we may face a problem. That is, names of vectors may get converted into row names, and data may be converted into a single colu 1 min read Convert DataFrame Column to Numeric in R In this article, we are going to see how to convert DataFrame Column to Numeric in R Programming Language. All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type conversion, 9 min read Convert List of Vectors to DataFrame in R In this article, we will discuss how to convert the given list of vectors to Dataframe using the help of different functions in the R Programming Language. For all the methods discussed below, few functions are common because of their functionality. Let us discuss them first. as.data.frame() is one 2 min read Convert dataframe to list of vectors in R In this article, we will learn how to convert a dataframe into a list of vectors, such that we can use columns of the dataframes as vectors in the R Programming language. Dataframe columns as a list of vectors as.list() function in R Language is used to convert an object to a list. These objects can 3 min read Like