Convert dataframe rows and columns to vector in R
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 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:
Convert data frame row to a vector
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:
Display the entire dataframe as vectors
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:





Please Login to comment...