如何追加行R的数据框 [英] How to append rows to an R data frame

查看:2149
本文介绍了如何追加行R的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,计算器,但我不能找到具体的我的问题,这涉及到追加行R的数据框的解决方案。

I have looked around StackOverflow, but I cannot find a solution specific to my problem, which involves appending rows to an R data frame.

我初始化一个空的2列的数据帧,如下:

I am initializing an empty 2-column data frame, as follows.

df = data.frame(x = numeric(), y = character())

然后,我的目标是通过值列表进行迭代,并且在每次迭代中,一个值追加到列表的末尾。我开始用下面的code。

Then, my goal is to iterate through a list of values and, in each iteration, append a value to the end of the list. I started with the following code.

for (i in 1:10) {
    df$x = rbind(df$x, i)
    df$y = rbind(df$y, toString(i))
}

我也试图功能 C 追加合并没有成功。请让我知道如果您有任何建议。

I also attempted the functions c, append, and merge without success. Please let me know if you have any suggestions.

推荐答案

不知道你正在尝试做的,我给大家一个建议:你要为每列的类型$ P ​​$ pallocate载体,将值插入这些载体,然后,到了最后,创建 data.frame

Update

Not knowing what you are trying to do, I'll share one more suggestion: Preallocate vectors of the type you want for each column, insert values into those vectors, and then, at the end, create your data.frame.

与朱利安 F3继续(A preallocated data.frame )作为最快的选项,到目前为止,定义为:

Continuing with Julian's f3 (a preallocated data.frame) as the fastest option so far, defined as:

# pre-allocate space
f3 <- function(n){
  df <- data.frame(x = numeric(n), y = character(n), stringsAsFactors = FALSE)
  for(i in 1:n){
    df$x[i] <- i
    df$y[i] <- toString(i)
  }
  df
}

下面是一个类似的方法,但一当 data.frame 创建为最后一步。

Here's a similar approach, but one where the data.frame is created as the last step.

# Use preallocated vectors
f4 <- function(n) {
  x <- numeric(n)
  y <- character(n)
  for (i in 1:n) {
    x[i] <- i
    y[i] <- i
  }
  data.frame(x, y, stringsAsFactors=FALSE)
}

微基准从微基准包将给予我们更多的COM prehensive洞察力比 system.time

microbenchmark from the "microbenchmark" package will give us more comprehensive insight than system.time:

library(microbenchmark)
microbenchmark(f1(1000), f3(1000), f4(1000), times = 5)
# Unit: milliseconds
#      expr         min          lq      median         uq         max neval
#  f1(1000) 1024.539618 1029.693877 1045.972666 1055.25931 1112.769176     5
#  f3(1000)  149.417636  150.529011  150.827393  151.02230  160.637845     5
#  f4(1000)    7.872647    7.892395    7.901151    7.95077    8.049581     5

F1()(下面的方法)是因为它要求多久令人难以置信的效率低下 data.frame 和因为越来越多对象的方法是在R通常很慢 F3()得多,由于preallocation改善,但 data.frame 结构本身可能是瓶颈的一部分在这里。 F4()试图绕过这个瓶颈不影响要采取的方法。

f1() (the approach below) is incredibly inefficient because of how often it calls data.frame and because growing objects that way is generally slow in R. f3() is much improved due to preallocation, but the data.frame structure itself might be part of the bottleneck here. f4() tries to bypass that bottleneck without compromising the approach you want to take.

这实在不是一个好主意,但如果你想要做这种方式,我想你可以试试:

This is really not a good idea, but if you wanted to do it this way, I guess you can try:

for (i in 1:10) {
  df <- rbind(df, data.frame(x = i, y = toString(i)))
}

请注意,在你的code,还有一个另一个问题:

Note that in your code, there is one other problem:


  • 您应该,如果你想的字符不会被转换为因素使用 stringsAsFactors 。用途: DF = data.frame(X =数字(),Y =字符(),stringsAsFactors = FALSE)

  • You should use stringsAsFactors if you want the characters to not get converted to factors. Use: df = data.frame(x = numeric(), y = character(), stringsAsFactors = FALSE)

这篇关于如何追加行R的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆