Honestly I am unsure if the title accurately describes what's happening, but here it goes.
Suppose I start with the following object "Tempcheck"
> str(Tempcheck)
'data.frame': 1872 obs. of 3 variables:
$ Time : POSIXlt, format: "2013-07-10 14:26:40" "2013-07-10 14:26:43" "2013-07-10 14:26:50" "2013-07-10 14:26:53" ...
$ rawTemp : int 107461 108551 109940 110258 110740 110890 111096 111164 111238 111296 ...
$ rawConductivity: int 969903 1287631 1298627 1292063 1303909 1297249 1305610 1297557 1305070 1298703 ...
I then call a function and use sapply to normalize some data.
TCalibration<- function(x){ #this function normalizes data based on the calculated y intercept and slope
dc <- (x*((Tempcor[[2]])))+((Tempcor[[1]])) # y = 1/m*x + -1/b
dc <- dc[[1]]
}
##calibrates rawTemp into real temp
Tempcheck$Temp <- sapply(Tempcheck[[2]],TCalibration)
Tempcor is a previous object that stores coefficients from a linear model. If this is relevant I can post it later.
> str(Tempcheck)
'data.frame': 1872 obs. of 4 variables:
$ Time : POSIXlt, format: "2013-07-10 14:26:40" ...
$ rawTemp : int 107461 108551 109940 110258 110740 110890 111096 111164 111238 111296 ...
$ rawConductivity: int 969903 1287631 1298627 1292063 1303909 1297249 1305610 1297557 1305070 1298703 ...
$ Temp : num 23.6 23.9 24.3 24.4 24.5 ...
This is all fine and dandy! UNTIL ....
I call another function
ConductivityCorrection <- function(x){
t <- 1+.02*(Tempcheck$Temp-25)
EC25 <- (x/t)
}
Then use sapply again to Tempcheck
Tempcheck$rawCEC <-sapply(Tempcheck[[3]] ,ConductivityCorrection)
I was expecting to get the same thing that I got with the previous line of code, but something strange happened.
str(Tempcheck$rawCEC)
num [1:1872, 1:1872] 998390 991974 983917 982090 979335 ...
The length of this vector is 1872^2 which I thought was odd. My suspision is that it comes from the line
t <- 1+.02*(Tempcheck$Temp-25)
I know i could do this a different way, but I'm trying to force my self to use the apply family and learn it better. Anyway any help would be appreciated. Thank you!
I am aware that this piece of code solves my problem.
Tempcheck$alphaT <- 1+.02*(Tempcheck$Temp-25)
Tempcheck$rawCEC <- Tempcheck[[3]]/Tempcheck$alphaT
I was looking for a way to turn this into a function and apply to each element in the column of Tempcheck[[3]]