This tutorial explains how to create a blank data set (data frame) with R.

dummydt=data.frame(matrix(ncol=0,nrow=0))

**Practical Application -**It is very useful when you append data sets in a loop. In the first iteration, it is required to form a structure of the data frame so that data from the subsequent iteration can be added to it.

**Check out the code below.**

unilogit2 = function(df,depvar, output) {

dummydt=data.frame(matrix(ncol=0,nrow=0))depvar1 = deparse(substitute(depvar))

out = deparse(substitute(output))

xxxx = names(df)[which(names(df)!= depvar1)]

for (i in 1:length(xxxx)) {

mylogit = glm(formula(paste(depvar1,"~",xxxx[i])), data = df, family = "binomial")

coeff = data.frame(summary(mylogit)$coefficient)

if (i==1) {output = rbind(dummydt,coeff)}else {output = rbind(output,coeff)}assign(out,output, envir = .GlobalEnv)

}

Code not working

ReplyDelete> dummydt=data.frame(matrix(ncol=0,nrow=0))

ReplyDelete> unilogit2 = function(df,depvar, output) {

+ dummydt=data.frame(matrix(ncol=0,nrow=0)) depvar1 = deparse(substitute(depvar))

Error: unexpected symbol in:

"unilogit2 = function(df,depvar, output) {

dummydt=data.frame(matrix(ncol=0,nrow=0)) depvar1"

> out = deparse(substitute(output))

> xxxx = names(df)[which(names(df)!= depvar1)]

Error in which(names(df) != depvar1) : object 'depvar1' not found

> for (i in 1:length(xxxx)) {

+ mylogit = glm(formula(paste(depvar1,"~",xxxx[i])), data = df, family = "binomial")

+ coeff = data.frame(summary(mylogit)$coefficient)

+ if (i==1) {output = rbind(dummydt,coeff)} else {output = rbind(output,coeff)} assign(out,output, envir = .GlobalEnv)

Error: unexpected symbol in:

" coeff = data.frame(summary(mylogit)$coefficient)

if (i==1) {output = rbind(dummydt,coeff)} else {output = rbind(output,coeff)} assign"

> }

Error: unexpected '}' in " }"

>