简体   繁体   English

R:在data.frame中将向量作为一行插入

[英]R: Insert a vector as a row in data.frame

Can I insert a vector as a row in a data.frame ? 我可以在data.frame插入一个vector作为行吗? If so how? 如果是这样的话?

I wouldn't claim this to be the most elegant and pretty solution out there, but it gets the job done. 我不认为这是最优雅和漂亮的解决方案,但它完成了工作。 Notice that each dataframe row carries its own row name, which becomes a problem when inserting new lines. 请注意,每个数据帧行都有自己的行名,这在插入新行时会出现问题。 That being said, you can mend this with row.names (see below). 话虽这么说,你可以用row.names来修补它(见下文)。

my.df <- data.frame(a = runif(10), b = runif(10), c = runif(10))
my.vec <- c(1, 1, 1)
new.df <- rbind(my.df[1:5, ], my.vec, my.df[6:nrow(my.df), ])
new.df
            a         b          c
1  0.45433791 0.3798105 0.84514864
2  0.07074529 0.4985765 0.53912585
3  0.09645574 0.5441647 0.96636213
4  0.60788436 0.6070706 0.53791603
5  0.01593911 0.1697248 0.62697924
6  1.00000000 1.0000000 1.00000000
61 0.98455694 0.2206702 0.85500531
7  0.85356834 0.5279596 0.27462326
8  0.48028935 0.6689572 0.05428349
9  0.95675901 0.6875491 0.77642924
10 0.24691330 0.7980741 0.24013096

row.names(new.df) <- 1:nrow(new.df)  # make row names pretty again

Make an R dataframe from vector, horizontally 从矢量水平地创建R数据帧

The key insight is to use the R transpose method: t(...) to transpose the vector before you pass it to the data.frame constructor. 关键的见解是使用R转置方法: t(...)在将向量传递给data.frame构造函数之前转置向量。

my_name_vector      = c("penguin1", "penguin2", "penguin3", "penguin4");
my_data_vector      = c("Skipper",  "Kowalski", "Rico",     "Private");
supplemental_vector = c("Mumble",   "Dorthy",   "Norma",    "Memphis");

#create a data frame out of a transposed vector
penguins = as.data.frame(t(my_data_vector));
#change the names of the dataframe to be the titles
colnames(penguins) <- my_name_vector;

supplemental_data_frame <- data.frame(t(supplemental_vector));
colnames(supplemental_data_frame) <- my_name_vector;
supplemental_data_frame;

#rbind means row bind, pass in two data.frame
penguins <- rbind(penguins, supplemental_data_frame);
penguins;

Prints: 打印:

   penguin1 penguin2 penguin3 penguin4
1  Mumble   Dorthy   Norma    Memphis

   penguin1 penguin2 penguin3 penguin4
1  Skipper  Kowalski Rico     Private
2  Mumble   Dorthy   Norma    Memphis

The rbind method is very inefficient, so if you're doing this more than a few hundred rows, expect to wait a long time. rbind方法的效率非常低,所以如果你这样做的行数超过几百行,就要等待很长时间。 If you need to be lightning quick then you need to pre-allocate space or use the list method as shown here: https://stackoverflow.com/a/20689857/445131 如果您需要快速闪电,则需要预先分配空间或使用列表方法,如下所示: https//stackoverflow.com/a/20689857/445131

rbind is good, but really tricky though as to handle the exact row number before and after. rbind很好,但是为了处理前后的确切行号真的很棘手。 A more rapid way is to use insertRow in the package miscTools . 更快的方法是在包miscTools使用insertRow

In the dataset example above, the code would be : 在上面的数据集示例中,代码将是:

my.df <- as.matrix(data.frame(a = runif(10), b = runif(10), c = runif(10)))
my.vec <- c(1, 1, 1)
new.df <- insertRow(my.df,7,my.vec)
new.df

Hope would be helpful. 希望会有所帮助。

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

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