I have a for my understanding rather simple problem, but I could not find a solution so far.
I have a dataset with 4 columns that start with time (time1, time2...) and 4 columns that start with count (count1, count2...).
#Step 1: Create data
time_cols <- data.frame(time1 = sample(1:50, 10, replace = TRUE),
time2 = sample(1:50, 10, replace = TRUE),
time3 = sample(1:50, 10, replace = TRUE),
time4 = sample(1:50, 10, replace = TRUE))
count_cols <- data.frame(count1 = sample(1:4, 10, replace = TRUE),
count2 = sample(1:4, 10, replace = TRUE),
count3 = sample(1:4, 10, replace = TRUE),
count4 = sample(1:4, 10, replace = TRUE))
# Step 2: Create the "id" column
id <- 1:10
# Step 3: Combine all the columns to form the dataframe
df <- cbind(time_cols, count_cols, id)
time1 time2 time3 time4 count1 count2 count3 count4 id
1 40 47 24 48 1 2 3 2 1
2 16 15 39 16 1 4 2 1 2
3 16 41 16 21 1 3 1 4 3
4 16 47 14 3 4 2 1 1 4
5 31 28 29 30 3 4 4 1 5
6 5 15 41 13 4 2 2 4 6
7 46 19 29 30 1 1 3 1 7
8 28 43 10 27 2 2 3 2 8
9 23 37 35 49 2 4 2 2 9
10 43 28 6 20 3 3 3 1 10
I want to reshape the dataset from wide to long and within one command: I want to gather the time columns into a variable column "timepoint" and a value column "time_count_value". At the same time, I also want to gather the count columns the same way into "count_no" and "count_value". The output should look like this:
timepoint time_count_value count_no count_value id
1 time1 40 count1 1 1
2 time2 47 count2 2 1
3 time3 24 count3 3 1
4 time4 48 count4 2 1
5 time1 16 count1 1 2
6 time2 15 count2 4 2
7 time3 39 count3 2 2
8 time4 16 count4 1 2
9 time1 16 count1 1 3
10 time2 41 count2 3 3
So I do not want to gather multiple columns into one (for this problem, there are multiple answers on stackoverflow), but in some way I want to perfom "two melt/gather functions simultaneously".
I tried the following with melt:
example_trials2 <- setnames(
reshape2::melt(example_trials2, measure = data.table:::patterns("^time", "^count"),
value.name = c("time_count_value", "count_value"),
variable.name = c("timepoint", "count_no")))
However, it first told me "could not find function "patterns". I therefore added data.table::: before patterns, now it says "Error: Pattern(s) not found". I also tried reinstalling data.table and installing an older version of the package, without success.
I am confident that this function is actually doing what I am searching for, but I don't get why it always throws an error with the regular expression/patterns.
So: I would be happy to hear how to fix the error with patterns or if you have a different idea for using melt or another package, e.g. pivot_longer.
Here is a base R option using
reshape+transformand you will see
data