[R] Dataframe modification
Gabor Grothendieck
ggrothendieck at myway.com
Tue Mar 8 17:19:51 CET 2005
Ramzi Feghali <ramzi_feg <at> yahoo.fr> writes:
:
: Hello,
: I have a problem and wish if anybody have a quick
: solution or function or if this question was asked
: before and you could give me the date to look in the
: archives for the response.
: My problem is that I have a dataframe D[663,40] with
: only one column with repeated values lines and a
: factor with 4 levels.
: I want to reorganize my dataframe, create a dataframe
: D1[x,40*4] and put the repeated values in one line so
: that all the factors will be in columns. For the
: values with a missing level factor I want to keep
: empty the concerning place.
: For example if i a have this dataframe:
:
: A B C
: 32 a 14
: 32 b 22
: 55 a 44
: 55 b 77
: 55 c 55
: 55 d 44
: 83 c 77
: 83 d 55
: 83 e 44
: I want to transform it to have this one:
:
: 32 a 14 b 22
: 55 a 44 b 77 c 55
: 83 c 77 d 55 e
44
:
Is this good enough?
R> # first solution
R> reshape(D, idvar="A", timevar="B", direction = "wide")
A C.a C.b C.c C.d C.e
1 32 14 22 NA NA NA
3 55 44 77 55 44 NA
7 83 NA NA 77 55 44
R> # second solution
R> xtabs(C ~ A + B, D)
B
A a b c d e
32 14 22 0 0 0
55 44 77 55 44 0
83 0 0 77 55 44
More information about the R-help
mailing list