I have a data frame of name
s, and each name
has a count
of individuals, e.g.
df = read.table(header=TRUE, text=
'name count
a 10
b 1
c 7
d 3')
> df
name count
1 a 10
2 b 1
3 c 7
4 d 3
I want to add another column with a list of '1'
s, as a tally
column, corresponding to the count
. So the output would look like this:
name count tally
1 a 10 c('1', '1', '1', '1', '1', '1', '1', '1', '1', '1')
2 b 1 c('1')
3 c 7 c('1', '1', '1', '1', '1', '1', '1')
4 d 3 c('1', '1', '1')
I was able to get close with a for
-loop, but I couldn't see a graceful way to re-join the result to the original data frame, and anyway, I would prefer a {tidyverse}
solution with dplyr::mutate()
.
You can try map
within mutate
> df %>%
+ mutate(tally = map(count, rep, x = 1))
name count tally
1 a 9 1, 1, 1, 1, 1, 1, 1, 1, 1
2 b 4 1, 1, 1, 1
3 c 7 1, 1, 1, 1, 1, 1, 1
4 d 1 1