R Program to convert a given data frame column(s) to a vector

How to convert a data frame column to a vector

Here we are explaining how to write an R program to convert a data frame column to a vector. For that we are using a built-in function data.frame(). This function helps to create data frames, tightly coupled collections of variables that share many of the properties of matrices and of lists. The syntax of this function is 

data.frame(…, row.names = NULL, check.rows = FALSE,check.names = TRUE, fix.empty.names = TRUE,stringsAsFactors = default.stringsAsFactors()) 

Where dots(...) indicates the arguments are of either the form value or tag = value and row. name is a NULL or a single integer or character string.

How to convert a data frame column to a vector in the R program

Below are the steps used in the R program to convert a data frame column to a vector. In this R program, we directly give the data frame to a built-in function. Here we are using the variables Df1, Df2, Df3, Df4 for holding data frames. Consider the variable A for holding the converted vector. The data frame is converted by using the function data.frame(). The function is done as like data.frame(Df1=5:9, Df2=1:5, Df3=10:14, Df4=15:19). Finally, print the result vector.


STEP 1: Assign the variables Df1, Df2, Df3, Df4 with data frames

STEP 2: Consider A for holding the result vector

STEP 3: Call the function data.frame() as data.frame(Df1=5:9, Df2=1:5, Df3=10:14, Df4=15:19) with data frames as arguments

STEP 4: Assign variable with the result of data.frame function

STEP 5: print the variable A which holding the result 

R Source Code

                                          Df1 = c(5, 6, 7, 8, 9)
Df2 = c(1, 2, 3, 4, 5)
Df3 = c(10, 11, 12, 13, 14)
Df4 = c(15, 16, 17, 18, 19)
A<- data.frame(Df1=5:9, Df2=1:5, Df3=10:14, Df4=15:19)


    Df1 Df2 Df3 Df4
1    5   1   10  15
2    6   2   11  16
3    7   3   12  17
4    8   4   13  18
5    9   5   14  19