Title: How to apply paired t.test or Wilcoxon test to my data
Tags: r;usage-statistics
Question: Let's start with a data:

```structure(list(Group = c("Mark", "Matt", "Tim", "Tom"), `1` = c(0.749552072382562, 
1.06820497349356, 1.00116263663573, 0.864987635002866), `2` = c(1.00839505250436, 
0.796306651704629, 1.02603677593328, 1.00321936833133), `3` = c(0.736638669191169, 
0.973483626272054, 1.14805519301778, 0.899272693725192), `4` = c(0.728882841159455, 
0.871211836418332, 1.0442119745299, 0.859935708928745), `5` = c(0.749552072382562, 
1.06820497349356, 1.00116263663573, 0.864987635002866), `6` = c(1.00839505250436, 
0.796306651704629, 1.02603677593328, 1.00321936833133), `7` = c(0.736638669191169, 
0.973483626272054, 1.14805519301778, 0.899272693725192), `8` = c(0.728882841159455, 
0.871211836418332, 1.0442119745299, 0.859935708928745), `9` = c(0.749552072382562, 
1.06820497349356, 1.00116263663573, 0.864987635002866), `10` = c(1.00839505250436, 
0.796306651704629, 1.02603677593328, 1.00321936833133), `11` = c(0.736638669191169, 
0.973483626272054, 1.14805519301778, 0.899272693725192), `12` = c(0.728882841159455, 
0.871211836418332, 1.0442119745299, 0.859935708928745), `13` = c(0.749552072382562, 
1.06820497349356, 1.00116263663573, 0.864987635002866), `14` = c(1.00839505250436, 
0.796306651704629, 1.02603677593328, 1.00321936833133), `15` = c(0.736638669191169, 
0.973483626272054, 1.14805519301778, 0.899272693725192), `16` = c(0.728882841159455, 
0.871211836418332, 1.0442119745299, 0.859935708928745), `17` = c(0.766036811789943, 
0.871085862829362, 1.02210371210681, 0.937452345474458), `18` = c(1.0357237385154, 
1.02805558505417, 0.946794300033338, 1.04688545274238), `19` = c(0.763210436944137, 
0.801397021884422, 0.952553568039278, 0.990226493248718), `20` = c(0.789338028300063, 
0.822815644347233, 0.958462750269733, 1.04183361434861), `21` = c(0.766036811789943, 
0.871085862829362, 1.02210371210681, 0.937452345474458), `22` = c(1.0357237385154, 
1.02805558505417, 0.946794300033338, 1.04688545274238), `23` = c(0.763210436944137, 
0.801397021884422, 0.952553568039278, 0.990226493248718), `24` = c(0.789338028300063, 
0.822815644347233, 0.958462750269733, 1.04183361434861), `25` = c(0.766036811789943, 
0.871085862829362, 1.02210371210681, 0.937452345474458), `26` = c(1.0357237385154, 
1.02805558505417, 0.946794300033338, 1.04688545274238), `27` = c(0.763210436944137, 
0.801397021884422, 0.952553568039278, 0.990226493248718), `28` = c(0.789338028300063, 
0.822815644347233, 0.958462750269733, 1.04183361434861), `29` = c(0.766036811789943, 
0.871085862829362, 1.02210371210681, 0.937452345474458), `30` = c(1.0357237385154, 
1.02805558505417, 0.946794300033338, 1.04688545274238), `31` = c(0.763210436944137, 
0.801397021884422, 0.952553568039278, 0.990226493248718), `32` = c(0.789338028300063, 
0.822815644347233, 0.958462750269733, 1.04183361434861), `33` = c(0.937894856206067, 
NA, 1.00383773624603, 1.04181193834546), `34` = c(1.03944921519508, 
NA, 0.983868286249464, 1.10409633668759), `35` = c(0.949802513948967, 
NA, 1.06522152108054, 1.04376827636719), `36` = c(0.965871712940006, 
NA, 1.18437146805406, 1.01355356488254), `37` = c(0.937894856206067, 
NA, 1.00383773624603, 1.04181193834546), `38` = c(1.03944921519508, 
NA, 0.983868286249464, 1.10409633668759), `39` = c(0.949802513948967, 
NA, 1.06522152108054, 1.04376827636719), `40` = c(0.965871712940006, 
NA, 1.18437146805406, 1.01355356488254), `41` = c(0.937894856206067, 
NA, 1.00383773624603, 1.04181193834546), `42` = c(1.03944921519508, 
NA, 0.983868286249464, 1.10409633668759), `43` = c(0.949802513948967, 
NA, 1.06522152108054, 1.04376827636719), `44` = c(0.965871712940006, 
NA, 1.18437146805406, 1.01355356488254), `45` = c(0.937894856206067, 
NA, 1.00383773624603, 1.04181193834546), `46` = c(1.03944921519508, 
NA, 0.983868286249464, 1.10409633668759), `47` = c(0.949802513948967, 
NA, 1.06522152108054, 1.04376827636719)), .Names = c("Group", 
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", 
"24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", 
"35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", 
"46", "47"), row.names = c(NA, 4L), class = "data.frame")
```

Each row is a collection of ratio which I got from comparison of two groups. I would like to know if the ratios are significantly different than 1. So, I would like to test if each row (vector) is different than 1 by using two tests mentioned in a title. How to apply those test to my data ? Please consider that each row may have a different length. ```NAs``` should be ignored. As and output I would like to have a table with 3 columns: ```Group name```, ```p-value t-test```, ```p.value Wilcoxon```. 

Can someone help mi with that ?


Here is the accepted answer: There might be a way to use the rows of the original data frame you have, but I'd strongly recommend to work with columns (tidy data frame).

```library(dplyr)
library(tidyr)

# assuming this is the name of your original dataset
dt

# reshape to create a column for each name
dt2 = data.frame(t(dt), stringsAsFactors = F)
names(dt2) = dt2[1,]
dt2 = dt2[-1,]
dt2[,names(dt2)] = sapply(dt2[,names(dt2)], as.numeric)

# reshape to create a column of names and values
dt3 = dt2 %&gt;% 
  gather(name,value,Mark:Tom) %&gt;%
  filter(!is.na(value))   # remove NAs

dt3 %&gt;%
  group_by(name) %&gt;%     # for each name
  summarise(pval_ttest = t.test(value, mu=1, data=.)$p.value,      # calculate t test p value
            pval_wilc = wilcox.test(value, mu=1, data=.)$p.value)  # calculate Wilcoxon p value


# # A tibble: 4 × 3
#    name   pval_ttest    pval_wilc
#   <chr&gt;        <dbl&gt;        <dbl&gt;
# 1  Mark 4.408038e-09 1.020895e-06
# 2  Matt 6.679416e-06 2.502045e-04
# 3   Tim 1.777060e-02 6.932590e-02
# 4   Tom 2.433548e-01 5.148382e-01
```

Some additional info about how a paired t test "understands" the measurements you give it and why differences and ratios might give different results.
Consider the following examples:

```# paired t test of 2 vectors of same size (before and after treatment)
# it compares the means of those vectors
t.test(1:10, 13:4, paired = T)

# Paired t-test
# 
# data:  1:10 and 13:4
# t = -1.5667, df = 9, p-value = 0.1516
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#   -7.331701  1.331701
# sample estimates:
#   mean of the differences 
# -3 


# t test that compares one vector's mean to 0
# that vector is the differences of the two initial vectors
t.test(1:10 - 13:4, mu=0)

# One Sample t-test
# 
# data:  1:10 - 13:4
# t = -1.5667, df = 9, p-value = 0.1516
# alternative hypothesis: true mean is not equal to 0
# 95 percent confidence interval:
#   -7.331701  1.331701
# sample estimates:
#   mean of x 
# -3 


# t test that compares one vector's mean to 1
# that vector is the ratios of the two initial vectors
t.test(1:10 / 13:4, mu=1)

# One Sample t-test
# 
# data:  1:10/13:4
# t = -0.46036, df = 9, p-value = 0.6562
# alternative hypothesis: true mean is not equal to 1
# 95 percent confidence interval:
#   0.3229789 1.4480623
# sample estimates:
#   mean of x 
# 0.8855206 
```

You can see that the paired t test is a simple t test of the differences' vector, which is possible as you have 2 vectors of the same length (before after treatment). It's not the same with a simple t test of the ratios' vector.

So, it's reasonable to have different results, but in some applications the ratio test is better. Check your bibliography on that.

Comment for this answer: The statistical test we're using here is not a paired t-test or Wilcoxon test, as both tests compare a set of values to 1 (pre-defined value). Paired test requires a direct comparison of a set of measurements of person X with another set of measurements of the same person X. Maybe the bibliography mentions that as a paired test, but it's not. The statistical tests have no idea about what those (ratio) values represent.
Comment for this answer: Practically you can compare each user's values with a vector of 1s. But, theoretically, what does this vector represent? You never measured those 1s. Also, still not paired test. See more about paired t tests here: http://www.biostathandbook.com/pairedttest.html . It seems to me that you can do a paired t test before you divide your measurements and compare the ratios to 1.
Comment for this answer: Also have a look at this: http://www.graphpad.com/support/faq/the-ratio-paired-t-test/ to see whether is better to consider differences or ratios. I'm sure that if the bibliography considers ratios there should be a good reason behind it.
Comment for this answer: Well, now I'm confused even more, as in the link you sent you mention "two group of people. One is a control group the other one was treated with a drug". That's definitely not a paired test, unless I'm missing something in the experiment design. In paired test you need one measurement or person X before the drug and one after the drug (same person). Then, it's a different story whether you'll investigate the difference of those values or the ratio. One story is "experiment design" and the other is "method of comparison".
Comment for this answer: If they represent the same "person" (double check that with someone that has experience on biological applications) then it's correct to use a paired t test to compare the differences of those measurements. Or, you can get the ratios and compare them to 1.
Comment for this answer: I've added a practical example of how things work with t test, paired t test and ratio t test. Hope it helps a bit more! :-)
Comment for this answer: Can we make a vector of `1`  with the same length as each of the row to compare the results using paired tests ?
Comment for this answer: I explained my experiment under this link: http://stats.stackexchange.com/questions/258295/ratio-between-control-and-treatment-siginificantly-different-than-1/258955#258955 . BTW. how to attach links in a nicer way ?
Comment for this answer: Just the last question if you do not mind. I will read attached documents. Can I use paired t-test before calculating ratio if I have let's say a pair of genes which first of all are done in four replicates and in general I would like to know how the specific group of genes change under a treatment.
Comment for this answer: I used group of people as an example. Clearly not a good one. I would refer cell cultures which were divided into two groups and one was used as a control a second was used for treatment. We can say that both groups represents the same "person". In that case I think paired test is the one which should be used.
Comment for this answer: Thanks, I really appreciate your help!
Comment for this answer: how do i include rowname such as each observation in my case the gene name if i have three comparison then what is the calculated p value between those comparison ? is there way to report it


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:02/03/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<r><usage-statistics>', 'Title': 'How to apply paired t.test or Wilcoxon test to my data', 'ViewCount': 551, 'id': 42022067, 'user_AboutMe': None, 'user_AccountId': 3403506.0, 'user_DisplayName': 'Shaxi Liver', 'user_Reputation': 1030.0, 'user_Views': 543.0, 'user_id': 2855161.0}}