Convert dataframe rows and columns to vector in R - GeeksforGeeks

05 Apr, 2021
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.
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 frame
Select column to be converted
Assign it to a variable
Display data frame so generated.
Syntax:
dataframe[[‘column’]]
Example:
R
# create vectorsid=c(7058,7084,7098)name=c('sravan','karthik','nikhil')  # passing into dataframedata=data.frame(id,name) print(data)  # convert id column into a vectorcolumn_data=data[['id']]print(column_data)  # convert name column into a vectorcolumn_data1=data[['name']]print(column_data1)
Output:
We can convert every row or entire dataframe by using a method called as.vector()
Approach
Create dataframe
Select row to be converted
Pass it to the function
Display 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 vectorsid=c(7058,7084,7098)name=c('sravan','karthik','nikhil')  # passing into dataframedata=data.frame(id,name) print(data)print("-----------")  # converting 1 st row to a vectoras.vector((data[1,]))print("-----------")  # converting 2nd row to a vectoras.vector((data[2,]))print("-----------")  # converting 3 rd row to a vectoras.vector((data[3,]))print("-----------")
Output:
Example: Using t 
R
# create vectorsid=c(7058,7084,7098)name=c('sravan','karthik','nikhil')  # passing into dataframedata=data.frame(id,name) print(data)print("-----------")  # converting 1 st row to a vectoras.vector(t(data[1,]))print("-----------")  # converting 2nd row to a vectoras.vector(t(data[2,]))print("-----------")  # converting 3 rd row to a vectoras.vector(t(data[3,]))print("-----------")
Output:
Example:
R
# create vectorsid=c(7058,7084,7098)name=c('sravan','karthik','nikhil')  # passing into dataframedata=data.frame(id,name) print(data)print("-----------")  # converting all dataframe  to a vectoras.vector(t(data))
Output:
Example 2 :
R
# create vectorsid=c(7058,7084,7098)address=c('guntur','hyd','kothapeta')name=c('sravan','karthik','nikhil')  # passing into dataframedata=data.frame(id,address,name) print(data)print("-----dataframe row to a vector------")  # converting dataframe to a vectoras.vector(t(data))  print("-----dataframe column to a vector------")  # converting dataframe 1 st  column to a vectordata1=data[['id']]print(data1)  # converting dataframe 2 nd  column to a vectordata1=data[['address']]print(data1)  # converting dataframe 3 rd  column to a vectordata1=data[['name']]print(data1)
Output:
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Loops in R (for, while, repeat)
Filter data by multiple conditions in R using Dplyr
How to change Row Names of DataFrame in R ?
Change Color of Bars in Barchart using ggplot2 in R
Remove rows with NA in one column of R DataFrame
How to change Row Names of DataFrame in R ?
Remove rows with NA in one column of R DataFrame
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?