Create a blank data.frame to receive results?

3

Suppose you want to create a blank data frame to receive the results of an adhesion test.

x<-data.frame(Distribuicao=c(NULL),p-value=c(NULL))

After the test, you have a vector with the results:

res.ad<-c("Gumbel", 0.9105)

To include the test results in the data frame I used rbind :

x<-rbind(x,res.ad)

In the result, x loses the names of the variables "Distribution" and "p-value", and x is:

XGumbel  X0.9105
 Gumbel   0.9105

How best to store these results or how can I get around the variable names?

    
asked by anonymous 01.12.2015 / 16:32

1 answer

6

The way you are doing now will bring problems.

See when you do:

res.ad <- c("Gumbel", 0.9105)

You are transforming the number into text:

res.ad
[1] "Gumbel" "0.9105"

Then in this part you would ideally work with a data.frame , for example, data.frame(Distribuição = "Gumbel", pvalue = 0.9105) .

That said, one way to merge results in this case is to work with lists. Go accumulating the results in a list and then add them all up by do.call with rbind . For example:

set.seed(1) # apenas para tornar o exemplo reproduzível

lista <- list() # lista para armazenar resultados

for(i in 1:5){
  # salva resultados
  lista[[i]] <- data.frame(Distribuicao = sample(letters, 1), pvalue = runif(1))
}

# junta todos resultados
do.call("rbind", lista)

The part of do.call with rbind can be replaced by functions of other packages such as bind_rows function dplyr or rbindlist data.table .

    
01.12.2015 / 16:50