I am using table() to create a frequency table for several values from the same data frame.
### make up some data
orig.df<-data.frame(
decade=c("1910s","1910s","1920s","1930s"),
size=c("low (<1)", "high (>5000)", "medium (11-100)","low (<1)"),
plant=c("orange","apple","orange","apple")
)
data.frame(rbind(
table(orig.df$decade),
table(orig.df$size, orig.df$decade),
table(orig.df$plant, orig.df$decade)
))
X1910s X1920s X1930s
2 1 1
high (>5000) 1 0 0
low (<1) 1 0 1
medium (11-100) 0 1 0
apple 1 0 1
orange 1 1 0
the Xs in the colnames can always be removed with gsub()
Problem is, that my data contains NAs and I am trying to highlight these explicitly. Somehowtable(..., useNA="always") creates a problem when rbinding multiple tables to a data frame.
data.frame(rbind(
table(orig.df$decade, useNA="always"),
table(orig.df$size, orig.df$decade, useNA="always"),
table(orig.df$plant, orig.df$decade, useNA="always")
))
X1910s X1920s X1930s NA.
X 2 1 1 0
high...5000. 1 0 0 0
low...1. 1 0 1 0
medium..11.100. 0 1 0 0
NA. 0 0 0 0
apple 1 0 1 0
orange 1 1 0 0
NA..1 0 0 0 0
I was thinking about using gsub() like for the colnames, but if you look closely "." can mean parenthesis, space and "-" so no luck with that.
desired output (basically the same table just with proper names, my real problem has several 100 rows, so I can't manually go through them all)
1910s 1920s 1930s
2 1 1
high (>5000) 1 0 0
low (<1) 1 0 1
medium (11-100) 0 1 0
NA 0 0 0
apple 1 0 1
orange 1 1 0
NA 0 0 0
What is the solution? There has to be some simple setting to change?