How i can create empty dataframe with column name where column is a vector any length
c("A","B","C")
Im try
df<-data.frame()
colnames(df)<-c("A","B")
But is not working
x <- LETTERS[1:3]
df <- as.data.frame(matrix(,0,length(x)))
names(df) <- x
str(df)
# 'data.frame': 0 obs. of 3 variables:
# $ A: logi
# $ B: logi
# $ C: logi
With OP's vector:
x <- c("field1","field2", "field3")
df <- as.data.frame(matrix(,0,length(x)))
names(df) <- x
str(df)
# 'data.frame': 0 obs. of 3 variables:
# $ field1: logi
# $ field2: logi
# $ field3: logi
as.data.frame(matrix(nrow=0,ncol=length(x),dimnames=list(NULL,x)))rbinding each row, starting with an empty dataframe with named columns, would make sense there.list before in which you put the data.frame returned from the external source. At the end of the loop, you rbind them all together with do.call. Or do like Roland suggested.
df <- data.frame(A = numeric(n), B = character(n), C = logical(n))for anyn.data.frame(A = c(), B = c())str(data.frame(A = c(), B = c())) 'data.frame': 0 obs. of 0 variables