I have data where the IDs of each observation are numbers stored as sequences usually in the form of X:Y, but sometimes concatenated lists. I would like to tidy the data so each observation has its own row so that I can then use a join function to add more descriptive IDs. Normally I'd use the gather()
function from tidyr
to do this but I'm having trouble unpacking the IDs as they are characters.
The data looks like this:
example <- data_frame(x = LETTERS[1:3], y = c("Condition 1", "Condition 2", "Condition 3"), z = c("1:3", "4:6", "c(7,9,10)"))
example
# A tibble: 3 × 3
x y z
<chr> <chr> <chr>
1 A Condition 1 1:3
2 B Condition 2 4:6
3 C Condition 3 c(7,9,10)
However these do not work and all produce NA
:
as.numeric("1:3")
as.integer("1:3")
as.numeric("c(7,9,10)")
as.integer("c(7,9,10)")
There must be a simple way to do this but I thought one long way might be to extract the numbers and store them as a list first. For the X:Y IDs I could do this by spliting the string at ":" and then creating a sequence from one number to the other like so:
example[1:2,] %>%
+ separate(z, c("a", "b"), sep = ":") %>%
+ mutate(a = as.numeric(a), b = as.numeric(b), new = list(seq(a, b)))
Error in eval(expr, envir, enclos) : 'from' must be of length 1
However this did not work.
What I'm aiming for looks like this:
# A tibble: 9 × 3
x y z
<chr> <chr> <dbl>
1 A Condition 1 1
2 A Condition 1 2
3 A Condition 1 3
4 B Condition 2 4
5 B Condition 2 5
6 B Condition 2 6
7 C Condition 3 7
8 C Condition 3 9
9 C Condition 3 10
What is the simplest way of achieving it?
We can create a vector with a sequence of numbers by using − if the sequence of numbers needs to have only the difference of 1, otherwise seq function can be used.
Creating a Multidimensional Array An array is created using the array() function. It takes vectors as input and uses the values in the dim parameter to create an array. A multidimensional array can be created by defining the value of 'dim' argument as the number of dimensions that are required.
We can use tidyverse
library(tidyverse)
example %>%
group_by(x) %>%
mutate(z = list(eval(parse(text=z)))) %>%
unnest
# x y z
# <chr> <chr> <dbl>
#1 A Condition 1 1
#2 A Condition 1 2
#3 A Condition 1 3
#4 B Condition 2 4
#5 B Condition 2 5
#6 B Condition 2 6
#7 C Condition 3 7
#8 C Condition 3 9
#9 C Condition 3 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With