Вопрос

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]]

Это было полезно?

Решение

The issue is that Tempcheck$Temp in your ConductivityCorrection function is a vector so t is a vector and thus x/t also returns a vector. Instead you can use mapply or sapply(seq_along(Tempcheck[[3]]), ...) and index both accordingly.

ConductivityCorrection <- function(x){
  t <- 1+.02*(Tempcheck$Temp[x]-25)  
  EC25 <- (Tempcheck$rawConductivity[x]/t) 
}

sapply(seq_along(Tempcheck$Temp, ConductivityCorrection)

Generally, if you're applying a function to every row in a data.frame, you can vectorize your solution and skip apply functions altogether:

Temcheck$Temp <- Tempcheck$rawTemp * Tempcor[[2]] + Tempcor[[1]]

Tempcheck$rawCEC <- Tempcheck$rawConductivity / (1 + 0.02 * (Tempcheck$Temp - 25))

However, for simpler functions like these, I really like the data.table syntax:

DT <- data.table(Tempcheck)

DT[, rawCEC := rawConductivity / (1 + 0.02*Temp - 25)]`)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top