-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueToRows.R
More file actions
46 lines (25 loc) · 803 Bytes
/
ValueToRows.R
File metadata and controls
46 lines (25 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Value n to n rows
# March 2023
# This script gives a toy example of converting from one row per plot to one row
# per insect. We often enter counts of insects per plot, but then we need to
# work with a data frame with one row per insect. So we need to use the value of
# a column to add rows.
# The solution uses the uncount function in the tidyr package in tidyverse
# Load packages ####
library(tidyverse)
# Set up data frame ####
d = data.frame(
plotID = rep(1:4, length=8),
date = rep(1:4, each=2),
daysToEmergance = rep(1:4 + 2, each=2),
devRate = rep(log(1/(1:4 + 2)), each=2),
trt = rep(letters[1:2], each=2, length=8),
adults = c(3,4,1,2,1,2,2,5)
)
# Repeat rows per number of adults ####
dl = d %>%
uncount(adults)
# Compare data frames ####
d
dl
# Works!