如何使用R中的头添加行来清空数据帧? [英] How to add rows to empty data frames with header in R?

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

问题描述


可能重复:

R:在空数据帧中添加行时丢失列名称


我创建了一个列数名字的空数据框,如下所示

 > compData<  -  data.frame(A = numeric(0),B = numeric(0))
> compData
[1] A B
< 0 rows> (或0长度行.names)
> compData< - rbind(compData,c(5,443))
> compData
X5 X443
1 5 443

在上面添加一行列名更改。如何将新的行数据添加到数据框?

解决方案

添加到零行数据.frame 将以不同的方式添加到已经包含行的 data.frame

?rbind


rbind数据帧方法首先将所有零列和零行参数。 (如果没有,则返回第一个参数,否则列为零列零行数据帧。)然后从第一个数据框中获取列的类,并按名称(而不是位置)匹配列, 。因素在必要时(按照所遇到的因素的级别的级别)扩大其水平,并且当且仅当所有组分都是有序因子时,结果才是有序因子。 (最后一点与S-PLUS不同)旧样式类别(具有级别的整数向量)被提升为因素。


你有很多选项 -



最直接的



  compData [1,]<  -  c(5,443)



更复杂



或者您可以将 c(5,433)强制转换为列表或data.frame

  rbind(compData,setNames(as.list(c(5,443)),names(compData)))

  rbind(compData,do.call(data.frame,setNames (as.list(c(5,443)),names(compData)))

但是在这你可能会这样做

  do.call(data.frame,setNames(as.list(c(5,443)),名称(compData)))



data.table选项



您可以使用 data.table 函数 rbindlist 这样做更少的检查和我们保留第一个数据的名称。框架

  library(data.table)
rbindlist(list(compData, as.list(c(5,443))


Possible Duplicate:
R: losing column names when adding rows to an empty data frame

I created an empty dataframe with column names only as follows

> compData <- data.frame(A= numeric(0), B= numeric(0))
> compData
[1] A B
<0 rows> (or 0-length row.names)
> compData <- rbind(compData,c(5,443))
> compData
  X5 X443
1  5  443

in the above after adding one row the column names are changed. How can I add new row data to data-frame?

解决方案

Adding to a zero-row data.frame will act differently to adding to an data.frame that already contains rows

From ?rbind

The rbind data frame method first drops all zero-column and zero-row arguments. (If that leaves none, it returns the first argument with columns otherwise a zero-column zero-row data frame.) It then takes the classes of the columns from the first data frame, and matches columns by name (rather than by position). Factors have their levels expanded as necessary (in the order of the levels of the levelsets of the factors encountered) and the result is an ordered factor if and only if all the components were ordered factors. (The last point differs from S-PLUS.) Old-style categories (integer vectors with levels) are promoted to factors.

You have a number of options --

the most straightforward

 compData[1, ] <- c(5, 443)

more complicated

Or you could coerce c(5,433) to a list or data.frame

rbind(compData,setNames(as.list(c(5,443)), names(compData)))

or

rbind(compData,do.call(data.frame,setNames(as.list(c(5,443)), names(compData))))

But in this case you might as well do

do.call(data.frame,setNames(as.list(c(5,443)), names(compData)))

data.table option

You could use the data.table function rbindlist which does less checking and thus preserves the names of the first data.frame

library(data.table)
rbindlist(list(compData, as.list(c(5,443))

这篇关于如何使用R中的头添加行来清空数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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