0

I'm new in R and what I want to do is something very simple but I need help.

img

I have a database that looks like the one above; where spot number = "name" of a protein, grupo = group I and II and APF = fluorescent reading. I want to do a tstudent test to each protein, by comparing groups I and II, but in a loop.

In the database above there only 1 protein (147) but im my real database i have 444 proteins.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • Welcome to SO. Please read [how to create a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – C8H10N4O2 Jul 18 '15 at 18:18
  • 1
    Considering your data is in a data frame called `gel`, this should work: `lapply(split(gel, gel$Spot.number), function(spot) t.test(APF ~ grupo, spot))` – Molx Jul 18 '15 at 22:06

2 Answers2

1

You can perform a t.test within each group using dplyr and my broom package. If your data is stored in a data frame called dat, you would do:

library(dplyr)
library(broom)

results <- dat %>%
    group_by(Spot.number) %>%
    do(tidy(t.test(APF ~ grupo, .)))

This works by performing t.test(APF ~ grupo, .) on each group defined by Spot.number. The tidy function from broom then turns it into a one-row data frame so that it can be recombined. The results data frame will then contain one row per protein (Spot.number) with columns including estimate, statistic, and p.value.

See this vignette for more on the combination of dplyr and broom.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • thanks for your answer. However, for all the protein, (i have tried 6 to start with, i get the same estimated (1,2) statistic, p-value, and so on...So something may be missing!? – Inês Diniz Jul 18 '15 at 20:43
  • should i give the data for one more protein to test it? – Inês Diniz Jul 18 '15 at 20:47
  • @InêsDiniz can you edit the result of `dput(head(dat, 100))` into your answer? That will create a reproducible example – David Robinson Jul 18 '15 at 21:38
  • David Robinson, i was doing something wrong with my database....i fix it and now everything fine and as it should be! thank you also – Inês Diniz Jul 19 '15 at 19:27
1

Starting with some fake data:

set.seed(0)
Spot.number <- rep(147:149, each=10)
grupo <- rep(rep(1:2, each=5), 3)
APF <- rnorm(30)
gel <- data.frame(Spot.number, grupo, APF)

> head(gel)
  Spot.number grupo        APF
1         147     1  2.1780699
2         147     1 -0.2609347
3         147     1 -1.6125236
4         147     1  1.7863384
5         147     1  2.0325473
6         147     2  0.6261739

You can use lapply to loop through the subsets of gel, split by the Spot.number:

tests <- lapply(split(gel, gel$Spot.number), function(spot) t.test(APF ~ grupo, spot))

or just

tests <- by(gel, gel$Spot.number, function(spot) t.test(APF ~ grupo, spot))

You can then move on to e.g. taking only the p values:

sapply(tests, "[[", "p.value")

#      147       148       149 
#0.2941609 0.9723856 0.5726007 

or confidence interval

sapply(tests, "[[", "conf.int")
#           147       148        149
# [1,] -0.985218 -1.033815 -0.8748502
# [2,]  2.712395  1.066340  1.4240488

And the resulting vector or matrix will already have the Spot.number as names which can be very helpful.

Molx
  • 6,816
  • 2
  • 31
  • 47
  • MOLX, thanks for your help, but i'm still with the same problem. Every protein has the same p-value!! The database you created is exactly like my database. When copy/past your script everything works fine, but when i use it in my database, every protein has the same p-value. So something is wrong with the "construction" of my database..... – Inês Diniz Jul 19 '15 at 19:20
  • MOLX, i was doing something wrong with my database....i fix it and now everything fine and as it should be! thank you ...life saver.. – Inês Diniz Jul 19 '15 at 19:25
  • 1
    @InêsDiniz Just a quick note: Your name and code suggest you are a native portuguese speaker. If that's true, check [Stack Overflow em Português](http://pt.stackoverflow.com/) some time. :) – Molx Jul 20 '15 at 03:07