[R] Implementing A Formula

MacQueen, Don macqueen1 at llnl.gov
Thu Jan 9 17:36:32 CET 2014


The phrase,
  "need to go row by row plugging this formula in to only take data from
that individual row"
suggests a spreadsheet-like concept. But R does not work the way a
spreadsheet does.

Given the example data and function that Frede provided (thank you!),
a simple, and probably the most basic, way would be

  mydata$cool2 <- myfun( mydata$a, mydata$b, mydata$c)

This also works:

  mydata$cool3 <- with(mydata, myfun(a,b,c))


> mydata
    a  b  c  d  e cool cool2 cool3
1   1 11 21 31 41   32    32    32
2   2 12 22 32 42   46    46    46
3   3 13 23 33 43   62    62    62
4   4 14 24 34 44   80    80    80
5   5 15 25 35 45  100   100   100
6   6 16 26 36 46  122   122   122
7   7 17 27 37 47  146   146   146
8   8 18 28 38 48  172   172   172
9   9 19 29 39 49  200   200   200
10 10 20 30 40 50  230   230   230


If you're new to R, and for the purpose of learning R, I'd suggest
understanding the simplest method before using the with() or do.call()
methods.


-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 1/7/14 10:18 PM, "Frede Aakmann Tøgersen" <frtog at vestas.com> wrote:

>Hi
>
>Is is something like this that you want?
>
>
>mydata <- data.frame(a=1:10, b=11:20, c=21:30, d=31:40, e=41:50)
>
>myfun <- function(a,b,c)a*b+c
>
>mydata$cool <- do.call(myfun, mydata[,c("a", "b", "c")])
>
>mydata
>##     a  b  c  d  e cool
>## 1   1 11 21 31 41   32
>## 2   2 12 22 32 42   46
>## 3   3 13 23 33 43   62
>## 4   4 14 24 34 44   80
>## 5   5 15 25 35 45  100
>## 6   6 16 26 36 46  122
>## 7   7 17 27 37 47  146
>## 8   8 18 28 38 48  172
>## 9   9 19 29 39 49  200
>## 10 10 20 30 40 50  230
>
>Yours sincerely / Med venlig hilsen
>
>
>Frede Aakmann Tøgersen
>Specialist, M.Sc., Ph.D.
>Plant Performance & Modeling
>
>Technology & Service Solutions
>T +45 9730 5135
>M +45 2547 6050
>frtog at vestas.com
>http://www.vestas.com
>
>Company reg. name: Vestas Wind Systems A/S
>This e-mail is subject to our e-mail disclaimer statement.
>Please refer to www.vestas.com/legal/notice
>If you have received this e-mail in error please contact the sender.
>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>> On Behalf Of Norman McBride
>> Sent: 7. januar 2014 23:46
>> To: r-help at r-project.org
>> Subject: [R] Implementing A Formula
>> 
>> Hello R Mailing List Members,
>>       My name is Peter. I am a high school student who is doing a senior
>> thesis in statistical analysis. As you can see that I have chosen R. So
>>for
>> the project I am working with a member of the US Navy and analyzing some
>> data. So for this data I am using a formula to find a cooling
>>coefficient.
>> This formula gets it own column in the table/display frame. So I wanted
>>to
>> know if put this formula into the display frame would I need to go row
>>by
>> row plugging this formula in to only take data from that individual row
>>or
>> would it just do that for me? If I did have to do it row by row would
>>there
>> be anyway I could expedite the process because it is over 2000 rows.
>>Thank
>> you for any help you are able to give me.
>> 
>>       -Peter
>> 
>> 	[[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> 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.
>
>______________________________________________
>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.




More information about the R-help mailing list