Skip to content

Excluding all missing values with dcast ("reshape2" package)

5 messages · Michael.Laviolette at dhhs.state.nh.us, arun, Jessica Streicher

#
Hello--I'm doing a simple crosstab using dcast:

rawfreq <- dcast(nh11brfs, race3~CHCCOPD, length)

with the results

               race3 Yes   No NA
1 White non-Hispanic 446 5473 21
2 Other non-Hispanic  29  211  0
3           Hispanic   6   81  1
4               <NA>  10   83  1

How would I modify this call to exclude all missing values; that is, to
obtain

               race3 Yes   No
1 White non-Hispanic 446 5473
2 Other non-Hispanic  29  211
3           Hispanic   6   81

Apologies if this has come up before, and thanks.

-M.L.
#
Hi,
?na.omit()
dat1<-read.table(text="
Tool Step_Number
A 1 
A 2 
A 3
A 3 
B 1 
B 2
B 2 
B 3
B NA
NA 3 
",sep="",header=TRUE,stringsAsFactors=FALSE)?? 
?dcast(na.omit(dat1),Tool~Step_Number,length)
#Using Step_Number as value column: use value.var to override.
?# Tool 1 2 3
#1??? A 1 1 2
#2??? B 1 2 1

A.K.





----- Original Message -----
From: "Michael.Laviolette at dhhs.state.nh.us" <Michael.Laviolette at dhhs.state.nh.us>
To: r-help at r-project.org
Cc: 
Sent: Monday, December 3, 2012 10:07 AM
Subject: [R] Excluding all missing values with dcast ("reshape2" package)


Hello--I'm doing a simple crosstab using dcast:

rawfreq <- dcast(nh11brfs, race3~CHCCOPD, length)

with the results

? ? ? ? ? ? ?  race3 Yes?  No NA
1 White non-Hispanic 446 5473 21
2 Other non-Hispanic? 29? 211? 0
3? ? ? ? ?  Hispanic?  6?  81? 1
4? ? ? ? ? ? ?  <NA>? 10?  83? 1

How would I modify this call to exclude all missing values; that is, to
obtain

? ? ? ? ? ? ?  race3 Yes?  No
1 White non-Hispanic 446 5473
2 Other non-Hispanic? 29? 211
3? ? ? ? ?  Hispanic?  6?  81

Apologies if this has come up before, and thanks.

-M.L.

______________________________________________
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.
#
This is not a reproducible example ;)

anyways, dcast has an attribute:
drop	
should missing combinations dropped or kept?

does that not do what you want?
On 03.12.2012, at 16:07, Michael.Laviolette at dhhs.state.nh.us wrote:

            
2 days later
#
Thanks for the response. The drop option of dcast was the first thing I
tried. Sorry for the lack of reproducibility, but my data set is large, I
couldn't find anything in the package examples, and I had hoped the
modification would be straightforward. Using dcast was my first choice
because it returns a data frame. I was successful using xtabs; there was
just the extra step of converting the returned table to a data frame.
Thanks again!

-M.L.

This is not a reproducible example ;)

anyways, dcast has an attribute:
drop
should missing combinations dropped or kept?

does that not do what you want?
On 03.12.2012, at 16:07, Michael.Laviolette at dhhs.state.nh.us wrote:

            
#
Thanks for the response. It turns out this will work if the data include no
other variables with missings; unfortunately, that was the case. I was
successful using xtabs with the extra step of converting the returned table
to a data frame. Thanks again!

-M.L.

Hi,
?na.omit()
dat1<-read.table(text="
Tool Step_Number
A 1
A 2
A 3
A 3
B 1
B 2
B 2
B 3
B NA
NA 3
",sep="",header=TRUE,stringsAsFactors=FALSE)
?dcast(na.omit(dat1),Tool~Step_Number,length)
#Using Step_Number as value column: use value.var to override.
?# Tool 1 2 3
#1??? A 1 1 2
#2??? B 1 2 1

A.K.





----- Original Message -----

Hello--I'm doing a simple crosstab using dcast:

rawfreq <- dcast(nh11brfs, race3~CHCCOPD, length)

with the results

? ? ? ? ? ? ?  race3 Yes?  No NA
1 White non-Hispanic 446 5473 21
2 Other non-Hispanic? 29? 211? 0
3? ? ? ? ?  Hispanic?  6?  81? 1
4? ? ? ? ? ? ?  <NA>? 10?  83? 1

How would I modify this call to exclude all missing values; that is, to
obtain

? ? ? ? ? ? ?  race3 Yes?  No
1 White non-Hispanic 446 5473
2 Other non-Hispanic? 29? 211
3? ? ? ? ?  Hispanic?  6?  81

Apologies if this has come up before, and thanks.

-M.L.