# [R] complex(?) reshaping question

Lauri Nikkinen lauri.nikkinen at iki.fi
Fri Dec 5 09:45:15 CET 2008

```Hello,

I have a problem with data reshaping. Here's my data

DF <-
structure(list(idvar1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("patient1", "patient2"
), class = "factor"), idvar2 = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("dob", "hog"), class =
"factor"),
eka1 = structure(c(NA, NA, NA, NA, NA, NA, NA, NA, 2L, 1L,
NA, NA, NA, NA), .Label = c("as", "df"), class = "factor"),
eka2 = structure(c(NA, NA, NA, NA, NA, NA, NA, NA, 2L, 1L,
NA, NA, NA, NA), .Label = c("as", "df"), class = "factor"),
eka3 = structure(c(NA, NA, NA, NA, NA, NA, NA, NA, 2L, 1L,
NA, NA, NA, NA), .Label = c("as", "df"), class = "factor"),
toka1 = structure(c(NA, NA, NA, NA, NA, NA, 1L, 2L, NA, NA,
NA, NA, NA, 2L), .Label = c("as", "fg"), class = "factor"),
toka2 = structure(c(NA, NA, NA, NA, NA, NA, 1L, 2L, NA, NA,
NA, NA, NA, 2L), .Label = c("as", "fg"), class = "factor"),
toka3 = structure(c(NA, NA, NA, NA, NA, NA, 1L, 2L, NA, NA,
NA, NA, NA, 2L), .Label = c("as", "fg"), class = "factor"),
kol1 = structure(c(NA, NA, NA, NA, 1L, 2L, NA, NA, NA, NA,
1L, 2L, NA, NA), .Label = c("hj", "ht"), class = "factor"),
kol2 = structure(c(NA, NA, NA, NA, 1L, 2L, NA, NA, NA, NA,
1L, 2L, NA, NA), .Label = c("hj", "ht"), class = "factor"),
kol3 = structure(c(NA, NA, NA, NA, 1L, 2L, NA, NA, NA, NA,
1L, 2L, NA, NA), .Label = c("hj", "th"), class = "factor")),
.Names = c("idvar1",
"idvar2", "eka1", "eka2", "eka3", "toka1", "toka2", "toka3",
"kol1", "kol2", "kol3"), class = "data.frame", row.names = c(NA,
-14L))

I'm trying to omit NA:s in this DF and produce a reduced DF. The
problem is that I cannot completely omit NA rows.

I tried

library(reshape)
g <- melt(DF, id=c("idvar1", "idvar2"))
g <- na.omit(g)
reshape(g, idvar=c("idvar1", "idvar2"), timevar="variable", direction="wide")

But this drops the second row. This is the resulting DF I should be getting

res <-
structure(list(idvar1 = structure(c(1L, 1L, 2L, 2L), .Label = c("patient1",
"patient2"), class = "factor"), idvar2 = structure(c(1L, 1L,
2L, 2L), .Label = c("dob", "hog"), class = "factor"), eka1 = structure(c(NA,
NA, 2L, 1L), .Label = c("as", "df"), class = "factor"), eka2 = structure(c(NA,
NA, 2L, 1L), .Label = c("as", "df"), class = "factor"), eka3 = structure(c(NA,
NA, 2L, 1L), .Label = c("as", "df"), class = "factor"), toka1 = structure(c(1L,
2L, NA, 2L), .Label = c("as", "fg"), class = "factor"), toka2 = structure(c(1L,
2L, NA, 2L), .Label = c("as", "fg"), class = "factor"), toka3 = structure(c(1L,
2L, NA, 2L), .Label = c("as", "fg"), class = "factor"), kol1 = structure(c(1L,
2L, 1L, 2L), .Label = c("hj", "ht"), class = "factor"), kol2 = structure(c(1L,
2L, 1L, 2L), .Label = c("hj", "ht"), class = "factor"), kol3 = structure(c(1L,
2L, 1L, 2L), .Label = c("hj", "th"), class = "factor")), .Names = c("idvar1",
"idvar2", "eka1", "eka2", "eka3", "toka1", "toka2", "toka3",
"kol1", "kol2", "kol3"), class = "data.frame", row.names = c(NA,
-4L))

Any ideas how to proceed?

Many thanks
Lauri

```