逐行创建R数据帧 [英] Creating an R dataframe row-by-row

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

问题描述

我想在R中逐行构造一个数据帧。我做了一些搜索,我想出的是建议创建一个空列表,保持列表索引的标量,然后每次添加列表中的单行数据帧并将列表索引提前一个。最后,列表中的 do.call(rbind,)



虽然这样工作,这似乎很麻烦。是否有更简单的方法实现同样的目标?



显然,我提到的情况我不能使用一些应用函数,并显式地需要逐行创建数据帧。至少有一种方法可以将 push 推送到列表的末尾,而不是明确跟踪使用的最后一个索引?

解决方案

您可以通过追加或使用 rbind()逐行增长。



这并不意味着你应该。动态增长的结构是在R中编码的最不有效的方法之一。



如果可以,只需保留所有的数据。框架:

  N < -  1e4#一些魔术数字,可能是高估

DF< - data.frame(num = rep (NA,N),txt = rep(,N),#根据需要
stringsAsFactors = FALSE)#你不知道级别

,然后在您的操作中插入行一次

  DF [i,]<  -  c(1.4,foo)

对于任意数据框架,并且效率更高。如果你超过N,你可以随时收缩空行。


I would like to construct a dataframe row-by-row in R. I've done some searching, and all I came up with is the suggestion to create an empty list, keep a list index scalar, then each time add to the list a single-row dataframe and advance the list index by one. Finally, do.call(rbind,) on the list.

While this works, it seems very cumbersome. Isn't there an easier way for achieving the same goal?

Obviously I refer to cases where I can't use some apply function and explicitly need to create the dataframe row by row. At least, is there a way to push into the end of a list instead of explicitly keeping track of the last index used?

解决方案

You can grow them row by row by appending or using rbind().

That does not mean you should. Dynamically growing structures is one of the least efficient ways to code in R.

If you can, just preserver all your data.frame up front:

N <- 1e4  # some magic number, possibly an overestimate

DF <- data.frame(num=rep(NA, N), txt=rep("", N),  # as many cols as you need
                 stringsAsFactors=FALSE)          # you don't know levels yet

and then during your operations insert row at a time

DF[i, ] <- c(1.4, "foo")

That should work for arbitrary data.frame and be much more efficient. If you overshot N you can always shrink empty rows out at the end.

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

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