0

I have two species and some values for them.

values <- c(1,2,3,4,5,6,7,8,9,10)
spp <- c(rep("a",5), rep("b",5))
df <- data.frame(spp, values, stringsAsFactor = FALSE)

I want to summarise the data frame, grouping by these species. My idea is summarise, getting a random value by species. Using dplyr philosophy, I want to do this:

n.df <- df %>%
group_by(spp) %>%
summarise(value = sample(value))

but the sample function isn't working into summarise

Does anyone have a solution?

Sotos
  • 51,121
  • 6
  • 32
  • 66
tales_alencar
  • 141
  • 1
  • 10
  • I don't know what "My idea is summarise, getting a random value by species." means. – Roland Dec 13 '18 at 13:13
  • `sample(value)` permutes the vector rather than returning a single value. Perhaps you want `sample(value,1)`? Also, you seem to have a typo -- is it `values` or `value`? If the former, you would need `sample(values,1)` – John Coleman Dec 13 '18 at 13:14

1 Answers1

1

Since you are using dplyr you can also take advantage of sample_n function, i.e.

library(dplyr)

df %>%
   group_by(spp) %>%
   sample_n(1)

which gives,

# A tibble: 2 x 2
# Groups:   spp [2]
  spp   values
  <chr>  <dbl>
1 a          2
2 b          9
Sotos
  • 51,121
  • 6
  • 32
  • 66