[R] trouble converting an array to a dataframe
Christopher W. Ryan
cryan at binghamton.edu
Wed Jan 21 06:10:52 CET 2009
I start with a dataframe called xrays. It contains scores on films from
each of two radiologists. It is in "long" format. I used the reshape
package to melt a data frame and then cast it into "wide" format, one
line for each patient (identified by redlognumb) with scores from both
radiologists for a given patient on the same line.
I named the result of the casting xrays.data. It is an array. I'd like
it to be a three-variable dataframe, with one column for scores from
each of two radiologists, and one column for redlognumb (because I will
then need to merge it with another dataframe that has a column named
redlognumb.) As you can see below, the data.frame() function turns
xrays.data into a two-variable dataframe. How can I get three columns
(or variables) into my final dataframe?
Thanks.
> head(xrays)
redlognumb radiologis barrtotal
1 3 2 13
2 4 2 16
3 5 2 10
4 6 2 11
5 9 2 NA
6 10 2 NA
>melted.xrays <- melt(xrays, id=c("redlognumb","radiologis"))
>
> head(melted.xrays <- na.omit(melt(xrays,
id=c("redlognumb","radiologis"))))
redlognumb radiologis variable value
1 3 2 barrtotal 13
2 4 2 barrtotal 16
3 5 2 barrtotal 10
4 6 2 barrtotal 11
7 1 1 barrtotal 11
8 2 1 barrtotal 2
>
> cast(melted.xrays.2, redlognumb~radiologis~variable)
radiologis
redlognumb 1 2
1 11 NA
2 2 NA
3 12 13
4 16 16
5 12 10
. . . cut off for brevity . . .
> str(xrays.data)
int [1:42, 1:2, 1] 11 2 12 16 12 13 18 8 19 14 ...
- attr(*, "dimnames")=List of 3
..$ redlognumb: Named chr [1:42] "1" "2" "3" "4" ...
.. ..- attr(*, "names")= chr [1:42] "1" "2" "3" "5" ...
..$ radiologis: Named chr [1:2] "1" "2"
.. ..- attr(*, "names")= chr [1:2] "1" "80"
..$ variable : Named chr "barrtotal"
.. ..- attr(*, "names")= chr "1"
> data.frame(xrays.data)
X1.barrtotal X2.barrtotal
1 11 NA
2 2 NA
3 12 13
4 16 16
5 12 10
6 13 11
7 18 NA
8 8 NA
. . . . cut off for brevity . . .
--
Christopher W. Ryan, MD
SUNY Upstate Medical University Clinical Campus at Binghamton
40 Arch Street, Johnson City, NY 13790
cryanatbinghamtondotedu
PGP public keys available at http://home.stny.rr.com/ryancw/
"If you want to build a ship, don't drum up the men to gather wood,
divide the work and give orders. Instead, teach them to yearn for the
vast and endless sea." [Antoine de St. Exupery]
More information about the R-help
mailing list