-2

I need some help with data analysis.
I do have two datasets (before & after) and I want to see how big the difference is between them.

Before

11330    STAT1
2721    STAT2
52438    STAT3
6124    SUZY

After

17401    STAT1
3462    STAT2
0    STAT3
72    SUZY

Tried to group them with tapply(before$V1, before$V2, FUN=mean).
But as I am trying to plot it, on x axis am not getting the group name but number instead. How can I plot such tapplied data (frequency on Y axis & group name on X axis)?

Also wanted to ask what is the proper command in R to compare such datasets as I am willing to find the difference between them?


Edited

dput(before$V1)
c(11330L, 2721L, 52438L, 6124L)

dput(before$V2)
structure(1:4, .Label = c("STAT1", "STAT2", "STAT3","SUZY"),class = "factor")

pogibas
  • 24,254
  • 17
  • 63
  • 100
  • How can you compare these datasets by putting the group name on the x axis? You have two values for each. – David Robinson Sep 24 '12 at 14:59
  • 1
    I was willing to see the difference between two plots just by eyeballing, but I do understand that there is more decent way of doing this. – pogibas Sep 24 '12 at 15:02
  • What kind of "dataset" is this? I take it from the names that the data is paired (STAT1 in before matches STAT1 in after?) Could you post samples of your `before1` and `before2` datasets using `dput(before1)` and `dput(before2)`? – David Robinson Sep 24 '12 at 15:03

1 Answers1

5

Here are a couple of ideas.

This is what I think your data look like?

before <- data.frame(val=c(11330,2721,52438,6124),
                     lab=c("STAT1","STAT2","STAT3","SUZY"))
after <- data.frame(val=c(17401,3462,0,72),
                     lab=c("STAT1","STAT2","STAT3","SUZY"))

Combine them into a single data frame with a period variable:

combined <- rbind(data.frame(before,period="before"),
      data.frame(after,period="after"))

Reformat to a matrix and plot with (base R) dotchart:

library(reshape2)
m <- acast(combined,lab~period,value.var="val")
dotchart(m)

Plot with ggplot:

library(ggplot2)
qplot(lab,val,colour=period,data=combined)
Ben Bolker
  • 173,430
  • 21
  • 312
  • 389
  • 1
    Thanks for the help! It solved my plotting problems, but I also wanted to ask if it's possible to divide those two sets (before/after) to get at least how many times they differ from each other. – pogibas Sep 24 '12 at 16:39
  • do you mean computing the difference between them? If so, what's wrong with `after$val-before$val`? (For a bigger set you might want to be careful that the elements are in the same order, but for the data you've given us this should be fine ... ?) Otherwise, please edit your question to provide a more precise question (i.e., show us what the desired answer is for this particular mini-example) – Ben Bolker Sep 24 '12 at 17:14