reshape
лайки "."
, поэтому сначала мы вставляем единицу в переменные year*
.
names(d) <- gsub("year", "year.", names(d))
Теперь, после того как мы дали reshape
пропущенные столбцы и order
,
d$annualpercentage.2002 <- NA
d$year.1999 <- NA
d <- d[c(1, order(names(d)[-1]) + 1)]
Ваша идея работает путем определения различных сортировок столбцов в varying
в списке:
res <- reshape(d, varying=list(2:5, 6:9), direction="long", idvar="countrylabel",
times=1999:2002, v.names=c("annualpercentage", "year"))
res
# countrylabel time annualpercentage year
# Austria.1999 Austria 1999 NA NA
# Belgium.1999 Belgium 1999 NA NA
# Bulgaria.1999 Bulgaria 1999 -0.6806495 NA
# Estonia.1999 Estonia 1999 NA NA
# France.1999 France 1999 NA NA
# Germany.1999 Germany 1999 NA NA
# Switzerland.1999 Switzerland 1999 -1.8497570 NA
# Austria.2000 Austria 2000 -0.6033900 0.14970015
# Belgium.2000 Belgium 2000 NA -0.49201756
# Bulgaria.2000 Bulgaria 2000 0.8263925 -0.36320990
# Estonia.2000 Estonia 2000 NA -2.51032544
# France.2000 France 2000 NA 0.57800624
# Germany.2000 Germany 2000 NA -0.52295712
# Switzerland.2000 Switzerland 2000 0.2783076 0.25616728
# Austria.2001 Austria 2001 -2.6962484 -0.15375642
# Belgium.2001 Belgium 2001 1.3088577 0.72528621
# Bulgaria.2001 Bulgaria 2001 NA NA
# Estonia.2001 Estonia 2001 NA -0.05563662
# France.2001 France 2001 0.2224629 0.74205086
# Germany.2001 Germany 2001 NA -0.01185349
# Switzerland.2001 Switzerland 2001 0.8354322 -1.40826638
# Austria.2002 Austria 2002 NA NA
# Belgium.2002 Belgium 2002 NA 1.60874778
# Bulgaria.2002 Bulgaria 2002 NA NA
# Estonia.2002 Estonia 2002 NA 0.55866704
# France.2002 France 2002 NA -1.59866472
# Germany.2002 Germany 2002 NA -0.11217415
# Switzerland.2002 Switzerland 2002 NA NA
Данные
d <- structure(list(countrylabel = c("Austria", "Belgium", "Bulgaria",
"Estonia", "France", "Germany", "Switzerland"), annualpercentage.1999 = c(NA,
-2.58060150400384, -0.0623757258909573, 0.267776001395166, NA,
NA, 0.048219924249952), annualpercentage.2000 = c(NA, -0.249416955035044,
1.3525450891501, 1.04446768824697, NA, -0.0582347596434839, -0.891400228849837
), annualpercentage.2001 = c(1.82469277697851, NA, NA, 1.04231605324821,
NA, -0.900145118946308, -1.19320727433597), year2000 = c(0.633712375393134,
NA, 1.24760861316098, -0.092964787061478, -0.59403260962332,
NA, -0.650348234181285), year2001 = c(0.587318286831079, NA,
NA, 0.348890470222513, NA, NA, NA), year2002 = c(0.0645316087966406,
-0.279456557428068, NA, NA, -0.0627400036074545, 1.30419117694731,
-0.484654596062051)), row.names = c(NA, -7L), class = "data.frame")