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.
- Create data frame
- Select column to be converted
- Assign it to a variable
- Display data frame so generated.
Convert data frame row to a vector
We can convert every row or entire dataframe by using a method called as.vector()
- Create dataframe
- Select row to be converted
- Pass it to the function
- Display result
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.
Example: Using t
Display the entire dataframe as vectors
Example 2 :