[R] How to do t.test to rows of a dataframe using apply family function?

PIKAL Petr petr.pikal at precheza.cz
Tue Aug 26 14:21:26 CEST 2014


Hi
That is because do.call is one function and rbind another. For both functions you can find its help page and read:
rbind
vectors or matrices. These can be given as named arguments. Other R objects will be coerced as appropriate: see sections ‘Details’ and ‘Value’.

The type of a matrix result determined from the highest type of any of the inputs in the hierarchy raw < logical < integer < double < complex < character < list .
do.call
what

either a function or a non-empty character string naming the function to be called.

args

a list of arguments to the function call. The names attribute of args gives the argument names.

Actually inner lapply was not necessary and simple
do.call("rbind", lll)
gives the same result.
From what I understand from help page, when argument in rbind is list of lists it returns matrix of lists which are printed as
    [,1]   [,2]
lll List,9 List,9
do.call takes rbind and uses it on each node of list and this node is coerced to matrix, which is suitable for changing to data frame.
as.data.frame(do.call("rbind", lll))
It is some magic of R which I use although I do not know much about its internals. If you are interested, the source code is available.
Regards
Petr
From: my1stbox [mailto:my1stbox at 163.com]
Sent: Tuesday, August 26, 2014 12:24 PM
To: PIKAL Petr
Cc: R Help
Subject: Re: RE: RE: [R] How to do t.test to rows of a dataframe using apply family function?

Thank you so much!
Why do do.call(rbind,lapply(lll,rbind)) and rbind(lapply(lll,rbind)) act so differently? What is the tricky part of that?
> do.call(rbind,lapply(lll,rbind))
statistic parameter p.value conf.int estimate null.value alternative
[1,] 2.775282 37.99977 0.008509272 Numeric,2 Numeric,2 0 "two.sided"
[2,] -1.498133 37.31294 0.1425116 Numeric,2 Numeric,2 0 "two.sided"
method data.name
[1,] "Welch Two Sample t-test" "rnorm(20) and rnorm(20)"
[2,] "Welch Two Sample t-test" "rnorm(20) and rnorm(20)"
> rbind(lapply(lll,rbind))
[,1] [,2]
[1,] List,9 List,9

2014-08-26
________________________________

________________________________
发件人:PIKAL Petr <petr.pikal at precheza.cz<mailto:petr.pikal at precheza.cz>>
发送时间:2014-08-26 17:58
主题:RE: RE: [R] How to do t.test to rows of a dataframe using apply family function?
收件人:"my1stbox"<my1stbox at 163.com<mailto:my1stbox at 163.com>>
抄送:"R Help"<r-help at r-project.org<mailto:r-help at r-project.org>>

Hi
Please reply also to rhelp, maybe others can offer better answer.
Well, are you aware that by unlist you get all results as character values and not numbers?
Personally I would use lapply and do.call on final result to get it as data frame.
Here is example
Some model list data
lll<-vector("list",length=2)
lll
[[1]]
NULL
[[2]]
NULL
lll[[1]]<-t.test(rnorm(20), rnorm(20))
lll[[2]]<-t.test(rnorm(20), rnorm(20))
str(unlist(lll))
Named chr [1:22] "0.0091599930484716" "37.9972278626102" ...
- attr(*, "names")= chr [1:22] "statistic.t" "parameter.df" "p.value" "conf.int1" ...
do.call(rbind, lapply(lll, rbind))
     statistic   parameter p.value   conf.int  estimate  null.value alternative
[1,] 0.009159993 37.99723  0.9927394 Numeric,2 Numeric,2 0          "two.sided"
[2,] 0.1256267   32.93057  0.9007913 Numeric,2 Numeric,2 0          "two.sided"
     method                    data.name
[1,] "Welch Two Sample t-test" "rnorm(20) and rnorm(20)"
[2,] "Welch Two Sample t-test" "rnorm(20) and rnorm(20)"
Regards
Petr

From: my1stbox [mailto:my1stbox at 163.com]
Sent: Tuesday, August 26, 2014 10:44 AM
To: PIKAL Petr
Subject: Re: RE: [R] How to do t.test to rows of a dataframe using apply family function?

Hi Petr,
Thank you! I use unlist() and rbind() because I want the result to be in the form of a matrix.
Regards
Allen

2014-08-26
________________________________

________________________________
发件人:PIKAL Petr <petr.pikal at precheza.cz<mailto:petr.pikal at precheza.cz>>
发送时间:2014-08-26 14:33
主题:RE: [R] How to do t.test to rows of a dataframe using apply family function?
收件人:"my1stbox"<my1stbox at 163.com<mailto:my1stbox at 163.com>>,"R Help"<r-help at r-project.org<mailto:r-help at r-project.org>>
抄送:

Hi

If your function works for you why to bother with apply? If it does not give you required results, please post some data and show us what is expected.

I would remove unlist from your function and declare list for storing data, which seems to me more natural for storing results.

t.test.list=function(df,paired){
   lll<-vector("list", length=nrow(df))
   for(i in 1:nrow(df)){

 lll[[i]]=t.test(df[i,grp1],df[i,grp2],paired=paired)
   }
  names(lll)=rownames(df)
   lll
> }

Regards
Petr


> -----Original Message-----
> From: r-help-bounces at r-project.org<mailto:r-help-bounces at r-project.org> [mailto:r-help-bounces at r-
> project.org] On Behalf Of my1stbox
> Sent: Tuesday, August 26, 2014 3:26 AM
> To: R Help
> Subject: [R] How to do t.test to rows of a dataframe using apply family
> function?
>
> Hi All,
> How to do t.test  to rows (with two levels, or in other words, groups
> of samples) of a dataframe using apply family function? I have done
> that using function and loops. And my overall purpose is to calculate
> DE genes from two groups of miRNA microarray samples. And I know limma
> can do that, but it seems limma doesn't support paired t-test.
>
> t.test.df=function(df,paired){
>   df.t=c()
>   for(i in 1:nrow(df)){
>
> df.t=rbind(df.t,unlist(t.test(df[i,grp1],df[i,grp2],paired=paired)))
>   }
>   rownames(df.t)=rownames(df)
>   df.t
> }
>
> Bests!
> Allen Chiu
>
> 2014-08-26
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org<mailto:R-help at r-project.org> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.

	[[alternative HTML version deleted]]



More information about the R-help mailing list