PaoloCrosetto - 1 year ago 60

R Question

I have data from an experiment. We timed human decision making. We have a set of alternaitves (let's call them A,B,C,D) to repeatedly choose from over a 30second period, and we time the first, then second, then Nth choice (subject could change their mind). The data looks like this (time in millisecond):

`subject time choice`

1 2204 A

1 3673 B

1 8435 C

1 12640 B

1 24031 A

I would like to discretize and expand the data, in order to be able to get the option chosen at each second; defaulting to 0 each time there is no choice (yet). Ideally, it would look like this

`subject second choice`

1 1 0

1 2 0

1 3 A

1 4 B

1 5 B

1 6 B

1 7 B

1 8 B

1 9 C

1 10 C

1 11 C

1 12 C

1 13 B

...and so on up to second = 30.

A solution based on the tidyverse packages and dplyr pipes would be most welcome. But I am open to other solutions. Thanks!

Answer Source

```
library(dplyr)
library(tidyr)
library(zoo)
df %>%
mutate(time=ceiling(time/1000)) %>%
complete(subject, time=1:30) %>%
group_by(subject) %>%
mutate(choice = na.locf(choice, na.rm = FALSE))
```

**data**

```
df = structure(list(subject = c(1L, 1L, 1L, 1L, 1L), time = c(2204L,
3673L, 8435L, 12640L, 24031L), choice = c("A", "B", "C", "B",
"A")), .Names = c("subject", "time", "choice"), class = "data.frame", row.names = c(NA,
-5L))
```