[R] reshape2's dcast() Adds NAs to Data Frame

arun smartpink111 at yahoo.com
Thu Aug 9 05:49:19 CEST 2012


Hi,

I tried converting factors to character, but the results still has NAs.
convert.type1 <- function(obj,types){
    for (i in 1:length(obj)){
        FUN <- switch(types[i],character = as.character, 
                                   numeric = as.numeric, 
                                   factor = as.factor,
                   Date=as.Date.character,
                   logical=as.logical)    
        obj[,i] <- FUN(obj[,i])
    }
    obj
}

sample.melt1<-convert.type1(sample.melt,c("character","Date","character","character","logical","numeric","numeric","character","numeric"))
 str(sample.melt1)
#'data.frame':    715 obs. of  9 variables:
# $ site    : chr  "D-1" "D-1" "D-1" "D-1" ...
# $ sampdate: Date, format: "2007-12-12" "2007-12-12" ...
# $ era     : chr  "Post" "Post" "Post" "Post" ...
# $ param   : chr  "AgTot" "AlTot" "Alk" "AsTot" ...
# $ ceneq1  : logi  TRUE FALSE FALSE FALSE TRUE FALSE ...
# $ floor   : num  0 0.106 231 0.0113 0 100 0 1.43 0 0.0239 ...
# $ ceiling : num  1.30e-04 1.06e-01 2.31e+02 1.13e-02 5.00e-03 1.00e+02 2.00e-04 1.43 6.00e-03 2.39e-02 ...
# $ variable: chr  "quant" "quant" "quant" "quant" ...
# $ value   : num  1.30e-04 1.06e-01 2.31e+02 1.13e-02 5.00e-03 1.00e+02 2.00e-04 1.43 6.00e-03 2.39e-02 ...

sample.cast <- dcast(sample.melt1, site + sampdate + era + ceneq1 + floor + ceiling ~ param) 
head(sample.cast)
  #site   sampdate  era ceneq1   floor ceiling AgDis AgTot AlDis Alk AlTot AsDis
#1  D-1 2007-12-12 Post  FALSE 0.00132 0.00132    NA    NA    NA  NA    NA    NA
#2  D-1 2007-12-12 Post  FALSE 0.01130 0.01130    NA    NA    NA  NA    NA    NA
#3  D-1 2007-12-12 Post  FALSE 0.02390 0.02390    NA    NA    NA  NA    NA    NA
#4  D-1 2007-12-12 Post  FALSE 0.02530 0.02530    NA    NA    NA  NA    NA    NA
#5  D-1 2007-12-12 Post  FALSE 0.03480 0.03480    NA    NA    NA  NA    NA    NA
#6  D-1 2007-12-12 Post  FALSE 0.10600 0.10600    NA    NA    NA  NA 0.106    NA
  #---------------------------------------------
  #---------------------------------------------
  #SO4 SrDis TDS TlDis TlTot TSS Vdis ZnDis ZnTot
#1  NA    NA  NA    NA    NA  NA   NA    NA    NA
#2  NA    NA  NA    NA    NA  NA   NA    NA    NA
#3  NA    NA  NA    NA    NA  NA   NA    NA    NA
#4  NA    NA  NA    NA    NA  NA   NA    NA    NA
#5  NA    NA  NA    NA    NA  NA   NA    NA    NA
#6  NA    NA  NA    NA    NA  NA   NA    NA    NA

A.K.





----- Original Message -----
From: Rich Shepard <rshepard at appl-ecosys.com>
To: R help <r-help at r-project.org>
Cc: 
Sent: Wednesday, August 8, 2012 10:48 PM
Subject: Re: [R] reshape2's dcast() Adds NAs to Data Frame

On Wed, 8 Aug 2012, Jeff Newmiller wrote:

> The explanation is that this is normal and consistent with behavior of
> factors in general. If you don't want that, it is common to work with
> character data instead of factors, only converting to factor when needed.
> In most cases I invoke read.table with the as.is=TRUE argument and delay
> converting to factors until I need them. Other people convert from factor
> to character and back to factor to get rid of unwanted factor levels on an
> as-needed basis.

Jeff,

  First thing tomorrow I will research the difference between characters and
data; I assumed they were the same.

Thanks,

Rich

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list