[R] Implementing a Formula into a Column of a Data Frame

MacQueen, Don macqueen1 at llnl.gov
Fri Jan 17 17:11:37 CET 2014


Suggest:

mydat <- 
structure(list(`Time (Seconds)` = c(20.318162, 21.316219, 22.316277,
23.316334, 24.316391, 25.316448, 26.316505, 27.315562, 28.315619,
29.315677, 30.315734, 31.315791, 32.315848, 33.315906, 34.315963,
35.31602, 36.316077, 37.316134, 38.316192, 39.316249), `Battery Temperature
(Celsius)` = c(12.290895,
12.331806, 12.373601, 12.424348, 12.467361, 12.522784, 12.581674,
12.642473, 12.713441, 12.760907, 12.832591, 12.90363, 12.953117,
13.037519, 13.103664, 13.193626, 13.267647, 13.340661, 13.439084,
13.512353), `Air Temperature (Celsius)` = c(12.109872, 12.130341,
12.127966, 12.126647, 12.138111, 12.13635, 12.133192, 12.137278,
12.144941, 12.140494, 12.145511, 12.143312, 12.136964, 12.15125,
12.1607, 12.162753, 12.152606, 12.148803, 12.16181, 12.151917
), `Battery Heat (Watts)` = c(1.634698, 1.634698, 1.634882, 1.634703,
1.634614, 1.634435, 1.634435, 1.634252, 1.634252, 1.634163, 1.633435,
1.633801, 1.633444, 1.63344, 1.804659, 2.013801, 2.013905, 2.013905,
2.0139, 2.013107), `Cooling Coefficient` = c(1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("Time (Seconds)",
"Battery Temperature (Celsius)", "Air Temperature (Celsius)",
"Battery Heat (Watts)", "Cooling Coefficient"), row.names = c(NA,
20L), class = "data.frame")

## long names with parentheses in them look nice but are a pain to
## use in R code, so I'm simplifying them:
names(dat) <- c('time','btemp','atemp','bheat','cc')

##    -- quote --
## So the formula would specifically be:  Battery Heat (Watts) /
## ( Surface Area (Square Meters) * ( Battery Temperature (Celsius) - Air
## Temperature (Celsius) )
##    -- end quote --

## there appears to be a missing closing ")" in the formula,
## so adding one here

 ccfun <- function(sarea,bheat,btemp,atemp) {
   bheat / (sarea * (btemp-atemp) )
 }

## replace the cooling coefficient values in the data frame with
## the cooling coefficient for a surfce area of 2 square meters:
mydat$cc <-  ccfun(2, dat$bheat, dat$btemp, dat$atemp)

No need for do.call.


-- 
Don MacQueen

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





On 1/17/14 6:54 AM, "Norman McBride" <npmcbride0917 at gmail.com> wrote:

>Now, if I were to move the data over from an excel doc would that change
>anything. I do understand that that I would have to change it to a csv
>document and create a variable for it. Just to give you a more visual
>understanding of what I am doing I a portion of the head of the data is
>shown below.  So the formula would specifically be:  Battery Heat (Watts)
>/
>( Surface Area (Square Meters) * ( Battery Temperature (Celsius) - Air
>Temperature (Celsius) ) and that would be the value put into the Cooling
>Coefficient Column currently set at one. One final note, when using
>function() should I input variable such as a,b, c and then change then
>change them when using the do.call function or should I just do it with
>the
>column names the first time? Thank you for the help I really appreciate
>it.
>
>structure(list(`Time (Seconds)` = c(20.318162, 21.316219, 22.316277,
>23.316334, 24.316391, 25.316448, 26.316505, 27.315562, 28.315619,
>29.315677, 30.315734, 31.315791, 32.315848, 33.315906, 34.315963,
>35.31602, 36.316077, 37.316134, 38.316192, 39.316249), `Battery
>Temperature
>(Celsius)` = c(12.290895,
>12.331806, 12.373601, 12.424348, 12.467361, 12.522784, 12.581674,
>12.642473, 12.713441, 12.760907, 12.832591, 12.90363, 12.953117,
>13.037519, 13.103664, 13.193626, 13.267647, 13.340661, 13.439084,
>13.512353), `Air Temperature (Celsius)` = c(12.109872, 12.130341,
>12.127966, 12.126647, 12.138111, 12.13635, 12.133192, 12.137278,
>12.144941, 12.140494, 12.145511, 12.143312, 12.136964, 12.15125,
>12.1607, 12.162753, 12.152606, 12.148803, 12.16181, 12.151917
>), `Battery Heat (Watts)` = c(1.634698, 1.634698, 1.634882, 1.634703,
>1.634614, 1.634435, 1.634435, 1.634252, 1.634252, 1.634163, 1.633435,
>1.633801, 1.633444, 1.63344, 1.804659, 2.013801, 2.013905, 2.013905,
>2.0139, 2.013107), `Surface Area (Square Meters)` = c(0.00637115,
>0.00637115, 0.00637115, 0.00637115, 0.00637115, 0.00637115, 0.00637115,
>0.00637115, 0.00637115, 0.00637115, 0.00637115, 0.00637115, 0.00637115,
>0.00637115, 0.00637115, 0.00637115, 0.00637115, 0.00637115, 0.00637115,
>0.00637115), `Cooling Coefficient` = c(1, 1, 1, 1, 1, 1, 1, 1,
>1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("Time (Seconds)",
>"Battery Temperature (Celsius)", "Air Temperature (Celsius)",
>"Battery Heat (Watts)", "Surface Area (Square Meters)", "Cooling
>Coefficient"
>), row.names = c(NA, 20L), class = "data.frame")
>
>	[[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.




More information about the R-help mailing list