I'm using a data frame similar to this one:
set.seed(1)
test.df <- data.frame(Treatment= "CI",
period = seq(1, 3),
subject= 1,
X.1. = rnorm(6),
X.2. = rnorm(6),
X.3. = rnorm(6),
Y.1. = rnorm(6),
Y.2. = rnorm(6),
Y.3. = rnorm(6))
> test.df
Treatment period subject X.1. X.2. X.3. Y.1. Y.2. Y.3.
1 CI 1 1 -0.6264538 0.4874291 -0.62124058 0.82122120 0.61982575 1.35867955
2 CI 2 1 0.1836433 0.7383247 -2.21469989 0.59390132 -0.05612874 -0.10278773
3 CI 3 1 -0.8356286 0.5757814 1.12493092 0.91897737 -0.15579551 0.38767161
4 CI 1 1 1.5952808 -0.3053884 -0.04493361 0.78213630 -1.47075238 -0.05380504
5 CI 2 1 0.3295078 1.5117812 -0.01619026 0.07456498 -0.47815006 -1.37705956
6 CI 3 1 -0.8204684 0.3898432 0.94383621 -1.98935170 0.41794156 -0.41499456
And I want my data to look like the following:
Treatment period subject Game X Y
1 CI 1 1 1 -0.6264538 0.82122120
where Game is from 1:3 and this has been done for each group of c(Treatment, Period). But in real data, beside X and Y, there are about 16 other similar variables. Inspired by this post, i tried to do it in the following way:
final.df<- test.df %>%
group_by(Treatment, period) %>%
reshape(idvar=1:3, varying=4:ncol(test.df), sep=".", direction='long',times=1:3)
and I get the following error
Error in `row.names<-.data.frame`(`*tmp*`, value = paste(d[, idvar], times[1L],
: invalid 'row.names' length