R define dimensions of empty data frame
Just create a data frame of empty vectors:
collect1 <- data.frame(id = character(0), max1 = numeric(0), max2 = numeric(0))
But if you know how many rows you're going to have in advance, you should just create the data frame with that many rows to start with.
Create empty data frame with 200 rows and no columns
This can work (if the call to 2 functions is not considered 2 commands):
data.frame(matrix(, nrow=200, ncol=0))
#data frame with 0 columns and 200 rows
Edit: Another option is data.frame()[1:200, ]
:
data.frame()[1:200, ]
# data frame with 0 columns and 200 rows
Create an empty data.frame
Just initialize it with empty vectors:
df <- data.frame(Date=as.Date(character()),
File=character(),
User=character(),
stringsAsFactors=FALSE)
Here's an other example with different column types :
df <- data.frame(Doubles=double(),
Ints=integer(),
Factors=factor(),
Logicals=logical(),
Characters=character(),
stringsAsFactors=FALSE)
str(df)
> str(df)
'data.frame': 0 obs. of 5 variables:
$ Doubles : num
$ Ints : int
$ Factors : Factor w/ 0 levels:
$ Logicals : logi
$ Characters: chr
N.B. :
Initializing a data.frame
with an empty column of the wrong type does not prevent further additions of rows having columns of different types.
This method is just a bit safer in the sense that you'll have the correct column types from the beginning, hence if your code relies on some column type checking, it will work even with a data.frame
with zero rows.
Create empty data frame with column names by assigning a string vector?
How about:
df <- data.frame(matrix(ncol = 3, nrow = 0))
x <- c("name", "age", "gender")
colnames(df) <- x
To do all these operations in one-liner:
setNames(data.frame(matrix(ncol = 3, nrow = 0)), c("name", "age", "gender"))
#[1] name age gender
#<0 rows> (or 0-length row.names)
Or
data.frame(matrix(ncol=3,nrow=0, dimnames=list(NULL, c("name", "age", "gender"))))
How to create an empty dataframe with specified columns AND number of rows in R?
If I understand the question correctly, the following hack will do what the OP wants. It creates the number of rows by setting the row.names
attribute directly. And if a dataframe has row names, it must have the corresponding rows.
empty_df <- old_df[0, ]
attr(empty_df, 'row.names') <- 1:nrow(new_df)
str(empty_df)
#'data.frame': 300 obs. of 5 variables:
# $ Sepal.Length: num
# $ Sepal.Width : num
# $ Petal.Length: num
# $ Petal.Width : num
# $ Species : Factor w/ 3 levels "setosa","versicolor",..:
The dataframe empty_df
now has 300 rows.
Data creation code.
The test data creation code uses the built-in dataset iris
.
set.seed(1234)
old_df <- iris
new_df <- rbind(iris, iris)
new_df <- new_df[, sample(ncol(new_df))]
how do you create an empty data frame using a function in R?
I think this does what you want:
do.call(data.frame,setNames(lapply(xv,function(e) vector(typeof(e))),xv));
## [1] Users Transactions Workload
## <0 rows> (or 0-length row.names)
Edit: Thanks to @Zheyuan for making me think a little more about my solution. Since the input (as specified by the OP) is an atomic vector, it cannot contain heterogeneous data types, so my lapply()
call that generates a separate zero-length vector according to each input element's type offers no benefit. It would offer a benefit if xv
was a list, which can contain heterogeneous data types, but since xv
is also being used to set the names of the resulting data.frame, it would be very questionable if it contained non-character elements. So my solution is actually not as sensible as I thought it was.
Here's a more sensible solution using the do.call(data.frame,...)
pattern, which replaces the lapply()
call with rep()
:
do.call(data.frame,setNames(rep(list(character()),length(xv)),xv));
## [1] Users Transactions Workload
## <0 rows> (or 0-length row.names)
working with empty data frames in R
Here are 4 ways to grow your data.frame
:
col1 <- letters[1:3] # [1] "a" "b" "c"
col2 <- letters[4:6] # [1] "d" "e" "f"
1- Start by assigning the first column
df1 <- data.frame(col1,stringsAsFactors = FALSE)
df1$col2 <- col2
2- Grow a list first, convert afterwards
l2 <- list()
l2$col1 <- col1
l2$col2 <- col2
df2 <- data.frame(l2,stringsAsFactors = FALSE)
3- Define the data.frame with columns initiated with the right length:
df3 <- data.frame(col1 = character(3), col2 = character(3))
df3$col1 <- col1
df3$col2 <- col2
4- Set rownames when you define it so it has 0 column and n rows
df4 <- data.frame(row.names = 1:3)
df4$col1 <- col1
df4$col2 <- col2
Check that it's all equivalent:
identical(df1,df2) # [1] TRUE
identical(df1,df3) # [1] TRUE
identical(df1,df4) # [1] TRUE
Related Topics
How to Expand a Large Dataframe in R
Tidyr::Pivot_Wider() Reorder Column Names Grouping by 'Name_From'
Ggplot2: Problem with X Axis When Adding Regression Line Equation on Each Facet
How to Save the Wordcloud in R
Purrr:Map and Glm - Issues with Call
Are Eigenvectors Returned by R Function Eigen() Wrong
Warning "The Condition Has Length > 1 and Only the First Element Will Be Used"
R: Strptime() and Is.Na () Unexpected Results
Remove Unused Categorical Values Boxplot - R
Ggplot Legend - Scale_Colour_Manual Not Working
Draw Bloxplots in R Given 25,50,75 Percentiles and Min and Max Values
How to Specify the Size/Layout of a Single Plot to Match a Certain Grid in R
Replace Na with Previous and Next Rows Mean in R
Counting Occurrence of Particular Letter in Vector of Words in R
Why Does Is.Vector() Return True for List
Order X Axis Day Values in Ggplot2
Rselenium, Chrome, How to Set Download Directory, File Download Error