简体   繁体   English

在R中创建向量或数据框

[英]Creating a vector or data frame in R

Using R if I have some data sets as 如果我有一些数据集,请使用R

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. 我想将其加载到具有变量V1,V2和V3的4行3列的DataFrame中。

Expected data frame should be like 预期的数据帧应该像

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

You can create the row vectors you want with the c() function 您可以使用c()函数创建所需的行向量

>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 . 然后使用rbind (行绑定)将它们组合成一个matrix

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

Then you can convert the matrix into a dataframe like so: 然后,您可以像这样将matrix转换为dataframe

>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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM