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 empty data frame with column names specified in R?
Just create a data.frame
with 0 length variables
eg
nodata <- data.frame(x= numeric(0), y= integer(0), z = character(0))
str(nodata)
## 'data.frame': 0 obs. of 3 variables:
## $ x: num
## $ y: int
## $ z: Factor w/ 0 levels:
or to create a data.frame with 5 columns named a,b,c,d,e
nodata <- as.data.frame(setNames(replicate(5,numeric(0), simplify = F), letters[1:5]))
Create empty tibble/data frame with column names coming from a vector
You can create a named vector, vec
, where the first argument sets the type of column you want. The rep("", 3)
line says I want three character columns. Then the second argument is the vector of column names.
Use dplyr::bind_rows
to convert this into tibble with one row. Then [0, ]
selects zero rows, leaving it empty.
With this method, you can control the data type for each column easily.
library(dplyr)
vec <- setNames(rep("", 3), letters[1:3])
bind_rows(vec)[0, ]
# A tibble: 0 x 3
# ... with 3 variables: a <chr>, b <chr>, c <chr>
You can also use as_tibble
if you transpose the named vector. I guess I use bind_rows
because I usually have dplyr
loaded but not tibble
.
library(tibble)
vec <- setNames(rep("", 3), letters[1:3])
as_tibble(t(vec))[0, ]
# A tibble: 0 x 3
# ... with 3 variables: a <chr>, b <chr>, c <chr>
If you know all of the columns are of a single type (e.g., character), you can do something like this.
vec <- letters[1:3]
df <- bind_rows(setNames(rep("", length(vec)), vec))[0, ]
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.
Add empty columns to a dataframe with specified names from a vector
The problem with your code is in the line:
for(i in length(namevector))
You need to ask yourself: what is length(namevector)
? It's one number. So essentially you're saying:
for(i in 11)
df[,i] <- NA
Or more simply:
df[,11] <- NA
That's why you're getting an error. What you want is:
for(i in namevector)
df[,i] <- NA
Or more simply:
df[,namevector] <- NA
Create dataframe column name or list element with a variable
The reason is because It is not evaluated when you define it as the name of the variable in data.frame()
. Try data.frame(i = i, wesh = 3:4)
to see the difference. However, a workaround can be to use setNames
, i.e.
setNames(data.frame(1:2, 3:4), c(i, 'wesh'))
#same for lists
#setNames(list(1:2, 3:4), c(i, 'wesh'))
which gives,
yo wesh
1 1 3
2 2 4
Initialize an empty tibble with column names and 0 rows
Since you want to combine a list of tibbles. You can just assign NULL to the variable and then bind_rows
with other tibbles.
res = NULL
for(i in tibbleList)
res = bind_rows(res,i)
However, a much efficient way to do this is
bind_rows(tibbleList) # combine all tibbles in the list
In data frame, create columns with names from vector in R
You can do the assignment like this:
for (i in 1:length(v1)) {
for (j in 1:length(v2)) {
# use [[]] to access the column
df[[paste(v1[i], v2[j], sep='_')]] <- "some value"
# this would also work:
# df[, paste(v1[i], v2[j], sep='_')] <- "some value"
}
rm(j)
}
rm(i)
In case you literally want to fill all columns with "some_value"
this is even easier:
column.names <- as.vector(sapply(v1, function(x) paste(x, v2, sep='_')))
df[,column.names] <- 'some_value'
creating, directly, data.tables with column names from variables, and using variables for column names with :=
For the first question, I'm not absolutely sure, but you may want to try and see if fread
is of any help creating an empty data.table with named columns.
As for the second question, try
DT[, c(nameOfCols) := 10]
Where nameOfCols
is the vector with names of the columns you want to modify. See ?data.table
Related Topics
Calculate Multiple Aggregations on Several Variables Using Lapply(.Sd, ...)
Rscript Does Not Load Methods Package, R Does -- Why, and What Are the Consequences
Apply a Function Over Groups of Columns
Get_Map Not Passing the API Key (Http Status Was '403 Forbidden')
R Error "Sum Not Meaningful for Factors"
How to Parse Year + Week Number in R
In R, Use Gsub to Remove All Punctuation Except Period
Create Zip File: Error Running Command " " Had Status 127
Re-Ordering Bars in R's Barplot()
Making a Stacked Area Plot Using Ggplot2
How to Select Last N Observation from Each Group in Dplyr Dataframe
Identify All Objects of Given Class for Further Processing
Change Background and Text of Strips Associated to Multiple Panels in R/Lattice
Calculating Mean for Every N Values from a Vector
How to Make Dodge in Geom_Bar Agree with Dodge in Geom_Errorbar, Geom_Point