[R] reshape and split
Gabor Grothendieck
ggrothendieck at myway.com
Fri Dec 17 21:14:36 CET 2004
Patrick Hausmann <c18g <at> zfn.uni-bremen.de> writes:
:
: Dear R-users,
:
: I am trying to reshape the DF "dat2" in the "long" format,
: but can't figure out how to use the "split"-option:
:
: > dat2
: a.1995.z b.1995.z a.1996.z var
: 1 100.00000 100.00000 100.00000 Neue Anlagen insgesamt
: 2 40.09904 23.60890 40.88960 Neue Ausrüstungen
: 3 59.90096 76.39110 59.11040 Neue Bauten
:
: This should be the result:
:
: region time wz value var
: a 1995 z 100 Neue Anlagen insgesamt
: a 1995 z 40.09904 Neue Ausrüstungen
: a 1995 z 59.90096 Neue Bauten
: b 1995 z 100 Neue Anlagen insgesamt
: b 1995 z 23.60890 Neue Ausrüstungen
: b 1995 z 76.39110 Neue Bauten
: a 1996 z 100 Neue Anlagen insgesamt
: a 1996 z 40.88960 Neue Ausrüstungen
: a 1996 z 59.11040 Neue Bauten
:
: # Not working, sure "[.]" is missing
: reshape(dat2, dir="long", varying=1:3, split=list(regexp="[0-9][a-z]",
: include=T))
Its easiest to just code times= and v.names= arguments directly rather
than use split= to try to guess them (at which point reworking it into
the form you want is straight forward):
nm3 <- names(dat2)[1:3]
dat2.long <- reshape(dat2, dir = "long",
varying = list(nm3), times = nm3, v.names = "value")
# rework into desired form
with(dat2.long,
data.frame(
region = substr(time,1,1),
time = substr(time,3,6),
wz = substr(time,8,8),
value = value,
var = var
)
)
More information about the R-help
mailing list