Dag
Dag

Reputation: 589

How to insert a value in a table

I have aggregated a table from my datafile using this synthax:

sumtab <- as.data.frame(table(S$MONTH))
colnames(sumtab) <- c("Month", "Frq")
rownames(sumtab) <- c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
                    "Sep","Oct","Dec")

Resulting in this table sumtab:

    Month Frq
Jan   1     3
Feb   2     5
Mar   3    16
Apr   4    45
May   5    11
Jun   6    16
Jul   7    99
Aug   8   101
Sep   9    45
Oct  10   456
Dec  12   112

And this script produces a ggplot:

ggplot(sumtab, aes(x=Month,y=Frq),width=1.5) +
   scale_y_continuous(limit=c(0,17),expand=c(0, 0)) +
   geom_bar(stat='identity',fill="lightgreen",colour="black") +
   xlab("Month") + ylab("No of bears killed") +
   theme_bw(base_size = 11) +
   theme(axis.text.x=element_text(angle=0,size=9))

The problem is that there are no values for November in my data, and I need to somehow enter a zero for November in the table. Probably a simple thing for most of you, and I have tried to search in other questions , and I have googled and read the books, but been unable to find the correct synthax.Need a little help.

Adding rbind into the script:

sumtab <- as.data.frame(table(S$MONTH))
sumtab <- rbind(sumtab, c(11, 0))

produced this error message:

Warning message:
In `[<-.factor`(`*tmp*`, ri, value = 11) :
  invalid factor level, NA generated

ant this table:

   Var1 Freq
1     1    3
2     2    5
3     3    6
4     4   14
5     5    7
6     6    2
7     7   13
8     8   12
9     9    3
10   10    1
11   12    4
12 <NA>    0

So thanks @PaulH for your help, but I've probably used your help in a wrong way.

Upvotes: 0

Views: 54

Answers (1)

PaulH
PaulH

Reputation: 181

You could use the rbind command to add the November row:

sumtab <- rbind(sumtab, Nov = c(11, 0))

Good luck!

Upvotes: 1

Related Questions