如何在R中初始化空数据框(很多列) [英] How to initialize empty data frame (lot of columns at the same time) in R

查看:2987
本文介绍了如何在R中初始化空数据框(很多列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现如何初始化3或4维的空数据框。这就像

I found how to initialize an empty data frame with 3 or 4 dimensions. It's like

df <- data.frame(Date=as.Date(character()),
             File=character(), 
             User=numeric(), 
             stringsAsFactors=FALSE)

然而,最有效的方式是初始化一个空的data.frame,有很多列名。像

However, What's the most effective way to initialize an empty data.frame with a lot of column names. like

mynames <- paste("hello", c(1:10000))

我尝试的错误方式是:

df <- data.frame(mynames=numeric())

/ p>

Thanks a lot beforehand

推荐答案

也许这个 -

df <- data.frame(matrix(ncol = 10000, nrow = 0))
colnames(df) <- paste0("hello", c(1:10000))

和@ joran的建议 - df< - setNames(data.frame(matrix(ncol = nrow = 0)),paste0(hello,c(1:10000)))

And @joran's suggestion - df <- setNames(data.frame(matrix(ncol = 10000, nrow = 0)),paste0("hello", c(1:10000)))

这篇关于如何在R中初始化空数据框(很多列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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