Create a blank data.frame to receive results?

Asked

Viewed 1,608 times

3

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

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

After the test is performed a vector with the results:

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

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

x<-rbind(x,res.ad)

In the result, x is lost 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 issue of variable names?

1 answer

6


The way you’re doing now will bring trouble.

See that when you do:

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

You’re turning the number into text:

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

So in this part the ideal would be for you to work with a data.frame, for example, data.frame(Distribuição = "Gumbel", pvalue = 0.9105).

That being said, one way to join results in this case is to work with lists. Go accumulating the results in a list and then join them all doing a 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 other package functions like function bind_rows of dplyr or rbindlist of data.table.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.