Create an object that contains four words. Use the sample function to sample 1000 values of that object

Asked

Viewed 144 times

-2

...continue-Find out how to use the table function to know how many times each word was drawn.

I created the four words that are "hi" "hi" "hey" and "hi" but when I tried according to an example I saw on youtube went wrong because they are not vectors.

creation: pal<-c("oi","ola","hey","hi") sample(pal,1000,replace=TRUE)

execution:

head(pal) levels(pal)

that’s where it went wrong because in the example I saw the guy was working with vectors

inserir a descrição da imagem aqui

  • 1

    Post the code instead of an image. Helps test :)

2 answers

3

Look:

 pal <- c("oi","ola","hey","hi")
 > is.factor(c("oi","ola","hey","hi"))
 [1] FALSE
 > is.vector(c("oi","ola","hey","hi"))
 [1] TRUE

Thus pal is a vector and not a category vector, so levels() doesn’t work.

simply give the Ctrl+r on the line sample(pal,1000, replace=TRUE) from your scripts and see what happens.
Go ahead, we’ve all been n00bs.

2

Are you sure you copied the right code? It’s not something like:

pal <- c("oi","ola","hey","hi")
palavreado <- sample(pal,1000, replace=TRUE)
table(palavreado)
palavreado
hey  hi  oi ola 
251 245 240 264 
  • Thank you had forgotten and to allocate the sample to a variable. Beginner error :))

  • @bonyduque And by the way, when to use functions that choose randomly or generate random numbers, as sample, start by calling set.seed(numero inteiro). Thus the results will be reproducible.

Browser other questions tagged

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