Wide to Long Multiple Measures Each Time

wide to long multiple measures each time

This is pretty close and changing the names of columns should be within your skillset:

reshape(DF, 
varying=c(work= c(3, 7), play= c(4,8), talk= c(5,9), total= c(6,10) ),
direction="long")

EDIT: Adding a version that is almost an exact solution:

reshape(DF, varying=list(work= c(3, 7), play= c(4,8), talk= c(5,9), total= c(6,10) ), 
v.names=c("Work", "Play", "Talk", "Total"),
# that was needed after changed 'varying' arg to a list to allow 'times'
direction="long",
times=1:2, # substitutes number for T1 and T2
timevar="times") # to name the time col

Long to wide w/ two repeated measures

I think this will do it:

library(reshape)
m <- melt(DF)

Simplest, but time and score are in the opposite order from your example (in case it matters)

cast(m,id+sex~...)

Or more explicitly:

cast(m,id+sex~variable+time)

You can cut this down to a one-liner:

recast(DF,id+sex~...)

If you like you can use the newer reshape2 package instead of reshape, replacing cast with dcast (the version of recast included in reshape2 doesn't give the desired result.)

Convert wide data to long format for repeated measures/mixed models


dplyr

tidyr::pivot_longer(wide, -Participant, names_to = "Item", values_to = "Accuracy")
# # A tibble: 9 x 3
# Participant Item Accuracy
# <chr> <chr> <int>
# 1 P1 Banana 0
# 2 P1 Apple 0
# 3 P1 Orange 0
# 4 P2 Banana 1
# 5 P2 Apple 1
# 6 P2 Orange 1
# 7 P3 Banana 0
# 8 P3 Apple 0
# 9 P3 Orange 0

Data wrangling from wide to long format with multiple repeating columns of different types

You could do:

library(tidyr)
library(dplyr)

df_wider %>% pivot_longer(-id,
names_pattern = "(.*)_(\\d)",
names_to = c(".value", "cluster"))

# A tibble: 15 x 5
id cluster fruit date number
<int> <chr> <fct> <date> <int>
1 1 1 olive 2020-04-21 50
2 1 2 elderberry 2020-02-23 59
3 1 3 cherimoya 2020-03-07 9
4 2 1 jujube 2020-03-22 88
5 2 2 mandarine 2020-03-06 45
6 2 3 grape 2020-04-23 78
7 3 1 nut 2020-01-26 53
8 3 2 cantaloupe 2020-01-27 70
9 3 3 durian 2020-02-15 39
10 4 1 chili pepper 2020-03-17 60
11 4 2 raisin 2020-04-14 20
12 4 3 cloudberry 2020-03-11 4
13 5 1 honeydew 2020-01-04 81
14 5 2 lime 2020-03-23 53
15 5 3 ugli fruit 2020-01-13 26


Related Topics



Leave a reply



Submit