store result of loop in df

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

store result of loop in df

R help mailing list-2
Dear list,

I have a dataframe df:

df<-data.frame(x=c(5,32,18,3,17), n=c(11,200,432,20,60))

Now I want to run n=nrow binom.test() with x being the number of success and n
the number of trials and then store the results as a new VAR in df.

I tried

  for (i in 1:nrow(df)){
  df$VAR<-(binom.test(df[i,1],df[i,2],0.065))$estimate[[1]]
}

but bin does only contain the last result.

What is wrong with my code? Can anyone help?

Thank you in advance.

Alain

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: store result of loop in df

Berend Hasselman

> On 29 Jul 2016, at 10:52, Alain D. via R-help <[hidden email]> wrote:
>
> Dear list,
>
> I have a dataframe df:
>
> df<-data.frame(x=c(5,32,18,3,17), n=c(11,200,432,20,60))
>
> Now I want to run n=nrow binom.test() with x being the number of success and n
> the number of trials and then store the results as a new VAR in df.
>
> I tried
>
>  for (i in 1:nrow(df)){
>  df$VAR<-(binom.test(df[i,1],df[i,2],0.065))$estimate[[1]]
> }
>
> but bin does only contain the last result.
>
> What is wrong with my code? Can anyone help?
>

How about

for (i in 1:nrow(df)){
 df$VAR[i] <- (binom.test(df[i,1],df[i,2],0.065))$estimate[[1]]
}

Berend Hasselman

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: store result of loop in df

Jim Lemon-4
In reply to this post by R help mailing list-2
Hi Alain,
You are probably storing the result, replicated five times, in df$VAR.
Each cycle of the loop replaces the last value with the current value.
If you really want the entire output of binom.test in the last column:

multi.binom.test<-function(xs,ns) {
  reslist<-list()
 for(i in 1:length(xs)) reslist[[i]]<-binom.test(xs[i],ns[i])
 return(reslist)
}
df$VAR<-multi.binom.test(df$x,df$n)

I suspect that you probably want only the p.value element.

Jim


On Fri, Jul 29, 2016 at 6:52 PM, Alain D. via R-help
<[hidden email]> wrote:

> Dear list,
>
> I have a dataframe df:
>
> df<-data.frame(x=c(5,32,18,3,17), n=c(11,200,432,20,60))
>
> Now I want to run n=nrow binom.test() with x being the number of success and n
> the number of trials and then store the results as a new VAR in df.
>
> I tried
>
>   for (i in 1:nrow(df)){
>   df$VAR<-(binom.test(df[i,1],df[i,2],0.065))$estimate[[1]]
> }
>
> but bin does only contain the last result.
>
> What is wrong with my code? Can anyone help?
>
> Thank you in advance.
>
> Alain
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] store result of loop in df

Rolf Turner
In reply to this post by R help mailing list-2
On 29/07/16 20:52, Alain D. via R-help wrote:

> Dear list,
>
> I have a dataframe df:
>
> df<-data.frame(x=c(5,32,18,3,17), n=c(11,200,432,20,60))
>
> Now I want to run n=nrow binom.test() with x being the number of success and n
> the number of trials and then store the results as a new VAR in df.
>
> I tried
>
>   for (i in 1:nrow(df)){
>   df$VAR<-(binom.test(df[i,1],df[i,2],0.065))$estimate[[1]]
> }
>
> but bin does only contain the last result.
>
> What is wrong with my code? Can anyone help?

Try:

for (i in 1:nrow(df)){
   df$VAR[i] <- binom.test(df[i,1],df[i,2],0.065)$estimate[[1]]
}

(Actually I was amazed that this works without initializing df$VAR, but
it *does* work!  There is no limit to the wondrous nature of R!)

BTW -- calling your data frame "df" is a bad idea.  There is a built-in
function named "df", and on occasion the result of naming some other
object "df" can be mysterious errors accompanied by opaque error messages.

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: store result of loop in df

Ivan Calandra-4
In reply to this post by R help mailing list-2
Dear Alain,

The problem is that you save the results of each iteration in df$VAR. So
obviously, you overwrite df$VAR at each iteration.

What you need to do it to create an empty vector that contains the right
number of elements and then iteratively fill this list. You can then
combine df and that vector

That would do it:
results <- vector(mode="numeric", length=nrow(df))
for (i in seq_along(results)){
     results[i] <- binom.test(df[i,1],df[i,2],0.065)$estimate[[1]]

}
df$VAR1 <- results

You could also use apply():
foo <- function(x) binom.test(x[[1]],x[[2]],0.065)$estimate
df$VAR2 <- apply(df[,1:2], 1, FUN=foo)

identical(df$VAR1, df$VAR2)

HTH,
Ivan

--
Ivan Calandra, PhD
Scientific Mediator
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
[hidden email]
--
https://www.researchgate.net/profile/Ivan_Calandra
https://publons.com/author/705639/

Le 29/07/2016 à 10:52, Alain D. via R-help a écrit :

> Dear list,
>
> I have a dataframe df:
>
> df<-data.frame(x=c(5,32,18,3,17), n=c(11,200,432,20,60))
>
> Now I want to run n=nrow binom.test() with x being the number of success and n
> the number of trials and then store the results as a new VAR in df.
>
> I tried
>
>    for (i in 1:nrow(df)){
>    df$VAR<-(binom.test(df[i,1],df[i,2],0.065))$estimate[[1]]
> }
>
> but bin does only contain the last result.
>
> What is wrong with my code? Can anyone help?
>
> Thank you in advance.
>
> Alain
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] store result of loop in df

Ivan Calandra-4
In reply to this post by Rolf Turner

Le 29/07/2016 à 11:20, Rolf Turner a écrit :

> On 29/07/16 20:52, Alain D. via R-help wrote:
>> Dear list,
>>
>> I have a dataframe df:
>>
>> df<-data.frame(x=c(5,32,18,3,17), n=c(11,200,432,20,60))
>>
>> Now I want to run n=nrow binom.test() with x being the number of
>> success and n
>> the number of trials and then store the results as a new VAR in df.
>>
>> I tried
>>
>>   for (i in 1:nrow(df)){
>>   df$VAR<-(binom.test(df[i,1],df[i,2],0.065))$estimate[[1]]
>> }
>>
>> but bin does only contain the last result.
>>
>> What is wrong with my code? Can anyone help?
>
> Try:
>
> for (i in 1:nrow(df)){
>   df$VAR[i] <- binom.test(df[i,1],df[i,2],0.065)$estimate[[1]]
> }
>
> (Actually I was amazed that this works without initializing df$VAR,
> but it *does* work!  There is no limit to the wondrous nature of R!)
I was so sure that it wouldn't work that I haven't even tried!

>
> BTW -- calling your data frame "df" is a bad idea.  There is a built-in
> function named "df", and on occasion the result of naming some other
> object "df" can be mysterious errors accompanied by opaque error
> messages.
>
> cheers,
>
> Rolf Turner
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: store result of loop in df

Bert Gunter-2
In reply to this post by R help mailing list-2
What's wrong with:

result <- with(df, x/n)  ? Do I misunderstand?

(and what's the p = .065 or [[1]] have to do with the estimated prob,
as you are not actually doing any test?)

Finally, if you really do this sort of thing, ?mapply (and ?with) can
be useful; e.g. you could have rewritten your loop as:

> with(df,mapply(function(...)binom.test(...)$estimate,x,n,MoreArgs = list(p=.065)))

probability of success probability of success probability of success
            0.45454545             0.16000000             0.04166667
probability of success probability of success
            0.15000000             0.28333333


Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Jul 29, 2016 at 1:52 AM, Alain D. via R-help
<[hidden email]> wrote:

> Dear list,
>
> I have a dataframe df:
>
> df<-data.frame(x=c(5,32,18,3,17), n=c(11,200,432,20,60))
>
> Now I want to run n=nrow binom.test() with x being the number of success and n
> the number of trials and then store the results as a new VAR in df.
>
> I tried
>
>   for (i in 1:nrow(df)){
>   df$VAR<-(binom.test(df[i,1],df[i,2],0.065))$estimate[[1]]
> }
>
> but bin does only contain the last result.
>
> What is wrong with my code? Can anyone help?
>
> Thank you in advance.
>
> Alain
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.