创建指定列数的空数据框
df.empty <- data.frame(var1="",var2="",var3="")[-1,]
#create data frame with 0 rows and 3 columns
df.empty <- data.frame(matrix(ncol = 3, nrow = 0))
#provide column names
colnames(df.empty) <- c('var1', 'var2', 'var3')
df.empty<- data.frame(Doubles=double(),Integers=integer(),Factors=factor(),Logicals=logical(),Characters=character(),stringsAsFactors=FALSE)
通过向空数据框循环填充等长向量实现行追加
df <- data.frame() # create data frame with 0 columns and 0 rows
for (i in seq(5)) {
vectr <- c(1,2,3)
df <- rbind(df,vectr) # fill data
}
colnames(df) <- c('var1', 'var2', 'var3') # set colnames
> df
var1 var2 var3
1 1 2 3
2 1 2 3
3 1 2 3
4 1 2 3
5 1 2 3
网友评论