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

February 25, 2023, Learn eTutorial
1086

How to convert a data frame column to a vector using the R program?

For making an R program to convert a data frame column to a vector, we are using a built-in function data.frame(). This function helps to create data frames, variable collections that are tightly occupied which 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,)
```
```

Where,

• dots(...)  are arguments ehih may be value or tag = value
• row. name is a NULL or a single integer or character string.

In this R program, we directly give the data frame to a built-in function. Here we are using the variables Df1, Df2, Df3, and 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 data.frame(Df1=5:9, Df2=1:5, Df3=10:14, Df4=15:19). Finally, print the result vector.

ALGORITHM

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 the data.frame function

STEP 5: print the variable A which holds 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)
print(A)```
```

OUTPUT

```    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   ```

VIEW ALL
VIEW ALL