SalB
SalB

Reputation: 1

Creating a vector or data frame in R

Using R if I have some data sets as

X1 = {20,  -221,2022} 
X2 = {17.4,-191,1922}
X3 = {15.9,-231,2122}
X4 = {21,  -211,2101}

And I want to load it to a DataFrame with 4 rows and 3 columns having variables V1,V2, & V3.

Expected data frame should be like

DF= 
V1   V2   V3
20, -221,2022
17.4,-191,1922
15.9,-231,2122
21,  -211,2101

Upvotes: 0

Views: 473

Answers (1)

Zero Pancakes
Zero Pancakes

Reputation: 296

You can create the row vectors you want with the c() function

>X1 <- c(20, -221, 2022)
>X2 <- c(17.4, -191, 1922)
>X3 <- c(15.9, -231, 2122)
>X4 <- c(21, -211, 2101)

And then use rbind (row - bind) to combine these into a matrix.

>matrix1 <- rbind(X1, X2, X3, X4)

Then you can convert the matrix into a dataframe like so:

>DF <- as.data.frame(matrix1)
>DF

     V1   V2   V3
X1 20.0 -221 2022
X2 17.4 -191 1922
X3 15.9 -231 2122
X4 21.0 -211 2101

Upvotes: 1

Related Questions